【发布时间】: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