Flask App Using WTForms with SelectMultipleField

Flask returns request.form as a werkzeug MultiDict object. This is kind of like a dictionary, only with traps for the unwary.

http://flask.pocoo.org/docs/api/#flask.request http://werkzeug.pocoo.org/docs/datastructures/#werkzeug.datastructures.MultiDict

MultiDict implements all standard dictionary methods. Internally, it saves all values for a key as a list, but the standard dict access methods will only return the first value for a key. If you want to gain access to the other values, too, you have to use the list methods.

However, I think there's an easier way. Can you do me a favor and try replacing:

language =  request.form['language']

with

language =  form.language.data

and see if that's any different? WTForms should handle the MultiDict object and just return a list for you since you've bound form data to it.


I ran into this problem recently. I found that you can retrieve all the items selected using

request.form.getlist('...')

This evaluates to a list with all selected items in it. In your case, use:

languages = request.form.getlist('language')