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.

Asked By: Matteo

||

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.

Answered By: kartheek

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

Answered By: optimus_prime
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.