Reputation: 26889
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.
Upvotes: 19
Views: 17390
Reputation: 171
The form object has an iterator defined on it:
{% for field in form %}
<tr>
{% if field.type == "BooleanField" %}
<td></td>
<td>{{ field }} {{ field.label }}</td>
{% else %}
<td>{{ field.label }}</td>
<td>{{ field }}</td>
{% endif %}
</tr>
{% endfor %}
This is from https://wtforms.readthedocs.io/en/2.3.x/fields/#wtforms.fields.Field.type
Upvotes: 17