21

I've looked through the documentation, but for the life of me, I can't figure out how the request.form object in Flask is populated. The documentation says that it's filled with parsed form data from POST or PUT requests, but my form is dynamic so I don't necessarily know what fields exist when the POST request is sent - though I want to make sure I add the information from these fields to the database.

Some of the fields in the form are always there, but there will also be any number of extra fields from a list of about 60. How should I go about figuring out which of these additional fields are in the request and how should I get the data from them?

EDIT: My specific problem has been solved, but it's still worth asking how the request.form dictionary is populated. I found out the hard way that if a checkbox input is unchecked, there is no key added to the dictionary under its name, and trying to get the value of a key that does not exist from the dictionary results in a rather confusing and cryptic HTTP 400 BAD REQUEST error.

2
  • 1
    Which values appear in request.form depend on which data the browser send. The decision not to send a key when a checkbox is unchecked is made by the browser, not by Flask. Commented Jul 20, 2013 at 6:51
  • 1
    I think it's best if you open new questions for the new questions you have. Commented Jul 26, 2013 at 15:35

2 Answers 2

43

request.form returns a MultiDict object. Basically, it means that for 1 key, you could have multiple values. If you want to test what your form POST looks like, just do a quick print statement as follows

f = request.form
for key in f.keys():
    for value in f.getlist(key):
        print key,":",value

If you read the documentation for MultiDict, it says

"A MultiDict is a dictionary subclass customized to deal with multiple values for the same key which is for example used by the parsing functions in the wrappers. This is necessary because some HTML form elements pass multiple values for the same key."

http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.MultiDict

Sign up to request clarification or add additional context in comments.

1 Comment

I'd like to say (contribute) that, at a first glance, it's a little bit difficult to understand how the request.form is parsed just like a normal Python dictionary (although it returns a kind of dict with tuples if you try to print f), but the answer makes a lot of sense, specially regarding the example and also the definition of Flask MultiDict. Thanks a lot, #codegeek :).
0

I ran into the same problem. I fixed calling the get method of ImmutableMultiDict.

Here's my code for a 'is_delivery' fieldname:

 if form_data.get('is_delivery', False) == 'on':
    is_delivery = 1
else:
    is_delivery = 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.