Iterating over submitted form fields in Flask?

后端 未结 2 1584
Happy的楠姐
Happy的楠姐 2021-02-02 14:51

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 a

2条回答
  •  [愿得一人]
    2021-02-02 15:09

    The form object has an iterator defined on it:

    {% for field in form %}
        
        {% if field.type == "BooleanField" %}
            
            {{ field }} {{ field.label }}
        {% else %}
            {{ field.label }}
            {{ field }}
        {% endif %}
        
    {% endfor %}
    

    This is from https://wtforms.readthedocs.io/en/2.3.x/fields/#wtforms.fields.Field.type

提交回复
热议问题