Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over submitted form fields in Flask?

Tags:

python

flask

In Flask 0.8, I know I can access individual form fields using form.fieldname.data, but is there a simple way of iterating over all the form fields? I'm building an email message body, and I'd like to loop over all the fields and create a fieldname/value entry for each, as opposed to manually building it by naming each field and appending.

like image 809
urschrei Avatar asked Oct 15 '11 12:10

urschrei


1 Answers

I suspect that your are using WTForms.

You can iterate over form data:

for fieldname, value in form.data.items():
    pass

You can iterate over all form fields:

for field in form:
    # these are available to you:
    field.name
    field.description
    field.label.text
    field.data
like image 164
Ski Avatar answered Sep 29 '22 10:09

Ski