Dynamic form fields in flask.request.form Dynamic form fields in flask.request.form flask flask

Dynamic form fields in flask.request.form


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.formfor 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


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 = 1else:    is_delivery = 0