(How) can I indicate if the form field is hidden in the Django template

I have a set of Django forms, and some fields have hidden inputs.

I am trying to create headers from the first element in a formset using formset.visible_fields. It works.

<table> <tr> {% for myfield in formset.0.visible_fields %} <th> {{ myfield.name }}</th> {% endfor %} </tr> {%for form in formset %} <tr> {% for field in form %} <td>{{ field }}</td> {% endfor %} </tr> {% endfor%} </table> 

The problem is that hidden fields do not get the header. But when I repeat my form fields, the hidden field is still wrapped with a tag. Therefore, I get a column for each field, but the header is only for visible fields.

Is there a way to check in advance if my field is hidden? (Or is there a better way to hide the headers / fields?)

+5
source share
1 answer

In fact, hidden fields have a property. Here are the docs about them.

Code from documents:

 {# Include the hidden fields #} {% for hidden in form.hidden_fields %} {{ hidden }} {% endfor %} {# Include the visible fields #} {% for field in form.visible_fields %} <div class="fieldWrapper"> {{ field.errors }} {{ field.label_tag }} {{ field }} </div> {% endfor %} 
+15
source

Source: https://habr.com/ru/post/1205453/


All Articles