I have a multiselect in html file like this:
> In sometimes, If you are using Ajax POST method then check the parameter name in network tab. Check the image attached that describes how to verify the parameter names.
In Flask view:
you can access the list responce based on parameter name.
request.form.getlist('mymultiselect[]')
or
request.form.getlist('mymultiselect')
You want to use the getlist() function to get a list of values:
multiselect = request.form.getlist('mymultiselect')
You do not need to add []
to the name to make this work; in fact, the []
will not help, don't use it at all.
You can create a function to get values as dictionary
def __get_form_data(self, method='POST', compare_with=dict()):
# Get form MiniFieldStorage items
form = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ={ 'REQUEST_METHOD' : method })
# Convert to dictionary
return { key.rstrip('[]'):form.getlist(key) if key.endswith('[]') else form.getvalue(key) for key in form.keys() if compare_with.get(key)!=form.getvalue(key) }