如何在django模板中获取表单中字段的长度、大小或数量?

如何在django模板中获取表单中字段的长度、大小或数量?,django,templates,django-templates,Django,Templates,Django Templates,我想写一个这样的循环,这样我就可以在表中展开表单字段了: {% load widget_tweaks %} {% load mathfilters %} {% load get_range %} {% for k in form|length|div:5|floatformat|add:1|get_range %} <tr> {% for field in form %} {% if forloop.counter >= k|mul:5

我想写一个这样的循环,这样我就可以在表中展开表单字段了:

{% load widget_tweaks %}
{% load mathfilters %}
{% load get_range %}

{% for k in form|length|div:5|floatformat|add:1|get_range %}
    <tr>
        {% for field in form %}
        {% if forloop.counter >= k|mul:5 and forloop.counter <= k|mul:5|add:4 %}
            <th>{{ field.label_tag }}{{ field.errors }}</th>
        {% endif %}
        {% endfor %}
    </tr>
    <tr>
        {% for field in form %}
        {% if forloop.counter >= k|mul:5 and forloop.counter <= k|mul:5|add:4 %}
            <td>{{ field|add_class:"span4" }}</td>
        {% endif %}
        {% endfor %}
    </tr>
{% endfor %}

谢谢

我不喜欢这个代码,但这是我的第一个想法

{% for field in form %}

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

{% enfor %}
形式。我相信

{% for field_name in form.fields %}

我真的不确定你在找什么,但听起来是这样的:

{% 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 %}
{%用于表单%]中的字段
{%if-forloop.counter0 | divisibleby:5%}
{{field.label_tag}{{{field.errors}}
{%else%}
{{field.label_tag}{{{field.errors}}
{%endif%}
{%endfor%}
{%形式的字段为%}
{%if-forloop.counter0 | divisibleby:5%}
{field | add_class:“span4”}
{%else%}
{{field}}
{%endfor%}

谢谢你的建议-他们帮了我的忙!以下是最终对我有效的方法:

{% 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 %}
{%用于表单%]中的字段
{%if-forloop.counter0 | divisibleby:5%}
{%形式的字段为%}

{%if forloop.counter0>=forloop.parentloop.counter0和forloop.counter0=forloop.parentloop.counter0和forloop.counter0
{%cycle%}
有什么问题吗?你好,伊格纳西奥,非常感谢你的快速回复。我查阅了django手册,了解有关
{%cycle%}的更多信息
,我看不出这会有什么帮助。我仍然需要知道表单中有多少字段,以便我可以向表中添加正确的行数。您能举个例子说明我如何使用上面的
{%cycle%}
吗?如果
{%cycle%}
可以存储循环执行的次数,然后我可以使用两个循环-一个用于确定表单中的字段数,然后一个用于打印表单。Lalo,谢谢。从技术上来说,这回答了“如何获取表单中的字段数”的问题但这似乎并没有保留循环之外的值。使用我最初的方法,我需要以某种方式保存该值,以便在后续循环中使用它,我认为Django模板(按设计)不可能实现这一点。感谢@BurhanKhalid-我在最终使用的解决方案中使用了您的解决方案中的元素。
{% 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 %}