My problem is that with the given code:
from flask import Flask, request
app = Flask(__name__)
@app.route("/")
def hello():
return str(request.values.get("param", "None"))
app.run(debug=True)
and I visit:
http://localhost:5000/?param=a¶m=bbb
I should expect an output of ['a', 'bbb'] except Flask seems to only accept the first param and ignore the rest.
Is this a limitation of Flask? Or is it by design?
OR
OR
You can use
getlist
, which is similar to Django'sgetList
but for some reason isn't mentioned in the Flask documentation:The result is:
Use
request.args
if the param is in the query string (as in the question),request.form
if the values come from multiple form inputs with the same name.request.values
combines both, but should normally be avoided for the more specific collection.If you used
$('form').serialize()
in jQuery to encode your form data, you can userequest.form['name']
to get data, but when multiple input elements' names are the same,request.form['name']
will only get the first matched item. So I checked the form object from the Flask API, and I found this. Then I checked the MultiDict object, and I found the functiongetlist('name')
.If there are multiple inputs with the same name, try this method:
request.form.getlist['name']