python - Dynamic form fields in flask.request.form

ID : 274337

viewed : 33

Tags : pythonflaskwerkzeugpython





Top 3 Answer for python - Dynamic form fields in flask.request.form

vote vote

97

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

vote vote

88

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 
vote vote

70

Top 3 video Explaining python - Dynamic form fields in flask.request.form







Related QUESTION?