Django: For Loop to Iterate Form Fields

前端 未结 4 1002
半阙折子戏
半阙折子戏 2021-02-07 10:09

I don\'t want to use django\'s built in form generation, seeking to specify each field in my template in order to customize the html output.

How do I iterate over a ser

4条回答
  •  醉话见心
    2021-02-07 10:53

    The best way is to use two loops, one for hidden fields and one for visible fields :

    visibles:

    {% for field in form.visible_fields %}
        {{ field.label }}
        {{ field }}
    {% endfor %}
    

    hiddens:

    {% for hidden in form.hidden_fields %}
        {{ hidden }}
    {% endfor %}
    

    in this way you will have better control over UI elements.

提交回复
热议问题