[Answer]-How to I get the length or size or number of fields in a form in a django template?

0πŸ‘

βœ…

thanks for your suggestions – they helped! Here is what finally worked for me:

{% for field in form %}
    {% if forloop.counter0|divisibleby:5 %}
        <tr>
        {% for field in form %}
            {% if forloop.counter0 >= forloop.parentloop.counter0 and forloop.counter0 <= forloop.parentloop.counter0|add:4 %}
                <th>{{ field.label_tag }}{{ field.errors }} </th>
            {% endif %}
        {% endfor %}
        </tr>
        <tr>
        {% for field in form %}
            {% if forloop.counter0 >= forloop.parentloop.counter0 and forloop.counter0 <= forloop.parentloop.counter0|add:4 %}
                <td>{{ field }}</td>
            {% endif %}
        {% endfor %}
        </tr>
    {% endif %}
{% endfor %}

1πŸ‘

I’m really not sure what you are looking for, but it sounds like this:

{% for field in form %}
    <tr>
        {% if forloop.counter0|divisibleby:5 %}
            <th class="span4">{{ field.label_tag }}{{ field.errors }}</th>
        {% else %}
            <th>{{ field.label_tag }}{{ field.errors }}</th>
        {% endif %}
    </tr>
{% endfor%}
{% for field in form %}
    <tr>
        {% if forloop.counter0|divisibleby:5 %}
            <td>{{ field|add_class:"span4" }}</td>
        {% else %}
            <td>{{ field }}</td>
     </tr>
{% endfor %}
πŸ‘€Burhan Khalid

0πŸ‘

I dont like this code, but it was my first idea.

{% for field in form %}

    {% if forloop.last %}
        {{ forloop.counter }}
    {% endif %}

{% enfor %}
πŸ‘€Leandro

0πŸ‘

form.fields I believe.

{% for field_name in form.fields %}
πŸ‘€drew

Leave a comment