【问题标题】:How to I get the length or size or number of fields in a form in a django template?如何在 django 模板中获取表单中字段的长度或大小或数量?
【发布时间】:2013-09-03 19:47:30
【问题描述】:

我想写一个这样的循环,这样我就可以在表格中展开表单域。 :

{% 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 %}

这不起作用,但因为上面的代码在form|length 上失败了。为了使它起作用,我需要在模板中获取表单中的字段数。有谁知道如何做到这一点?我已经搜索了所有内容,但找不到任何东西。以下不起作用:

form.len
form.length
form|length

谢谢!

【问题讨论】:

  • {% cycle %} 有什么问题?
  • 您好伊格纳西奥,非常感谢您的快速回复。我查看了 django 手册以获取有关 {% cycle %} 的更多信息,但我看不出它会有什么帮助。我仍然需要知道表单中有多少字段,以便可以将正确数量的行添加到表中。你能举例说明我将如何使用上面的{% cycle %} 吗?如果{% cycle %} 可以存储循环执行的次数,那么我可以使用两个循环 - 一个来确定表单中的字段数,然后一个来打印表单。

标签: django templates django-templates


【解决方案1】:

我真的不确定您在寻找什么,但听起来像这样:

{% 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 %}

【讨论】:

  • 感谢@BurhanKhalid - 我在最终使用的解决方案中使用了您解决方案中的元素。
【解决方案2】:

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

{% for field in form %}

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

{% enfor %}

【讨论】:

  • 拉罗,谢谢。从技术上讲,这回答了“我如何获得表单中的字段数”的问题,但似乎这并没有保留循环之外的值。使用我原来的方法,我需要以某种方式保存该值以便在后续循环中使用它,我认为 Django 模板不可能(按设计)。
【解决方案3】:

form.fields 我相信。

{% for field_name in form.fields %}

【讨论】:

    【解决方案4】:

    感谢您的建议 - 他们帮助了!这是最终对我有用的方法:

    {% 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 %}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-18
      • 1970-01-01
      • 2017-07-18
      • 2017-03-22
      • 1970-01-01
      • 2011-02-04
      • 1970-01-01
      • 2020-06-09
      相关资源
      最近更新 更多