Iterating over submitted form fields in Flask?

后端 未结 2 1585
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:06

    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
    
    0 讨论(0)
  • 2021-02-02 15:09

    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

    0 讨论(0)
提交回复
热议问题