Django: For Loop to Iterate Form Fields Django: For Loop to Iterate Form Fields django django

Django: For Loop to Iterate Form Fields


Well this would clearly not work:

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

but this will:

{% for field in form %}    {{ field }}{% endfor %}


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.


This one should work :

{% for field in form %}  {{ field }}{% endfor %}

Once you loop through field in form , you can't access form.field