python flask ImmutableMultiDict
Question:
This is my code:
@user_bp.route('/band', methods=['GET', 'POST'])
def band_details():
from include.form.User import Banddetails
form = Banddetails()
if request.method == 'POST' and form.validate_on_submit():
pippo = request.args.getlist('name[]')
print 'sei passato di qui' + str(len(pippo))
for item in pippo:
print item
return "result"
return render_template("banddetails.html", form=form, session=session)
I have a similar form:
<input type="text" name="name[]" id="name" value="">
I want get the element name[]
, lastname[]
, … but I don’t understand the procedure described in the flask api.
Answers:
If you are using an HTTP POST method you need to retrieve parameters like this:
pippo = request.form.getlist('name[]')
If you use HTTP GET method, do it like this:
pippo = request.args.getlist('name[]')
Check the docs here.
you can also do the following:
d = request.form.to_dict()
update: see comments below – if you’re expecting an array of params, do the following:
d = request.form.to_dict(flat=False)
sources:
immutable multi-dict doc
to_dict doc
This is my code:
@user_bp.route('/band', methods=['GET', 'POST'])
def band_details():
from include.form.User import Banddetails
form = Banddetails()
if request.method == 'POST' and form.validate_on_submit():
pippo = request.args.getlist('name[]')
print 'sei passato di qui' + str(len(pippo))
for item in pippo:
print item
return "result"
return render_template("banddetails.html", form=form, session=session)
I have a similar form:
<input type="text" name="name[]" id="name" value="">
I want get the element name[]
, lastname[]
, … but I don’t understand the procedure described in the flask api.
If you are using an HTTP POST method you need to retrieve parameters like this:
pippo = request.form.getlist('name[]')
If you use HTTP GET method, do it like this:
pippo = request.args.getlist('name[]')
Check the docs here.
you can also do the following:
d = request.form.to_dict()
update: see comments below – if you’re expecting an array of params, do the following:
d = request.form.to_dict(flat=False)
sources:
immutable multi-dict doc
to_dict doc