Django:在表单字段之间显示文本
大家好,Stackoverflow,
我正在通过循环显示一个大表单:
<table>
{% for field in projectDetailForm %}
<tr>
<td> {{ field.label_tag }} </td>
<td> {{ field }} </td>
</tr>
{% endfor %}
</table>
我想在几个表单字段之后用表单字段中断表格以显示更多说明。由于表单相当大(20个字段),我想避免每个表单字段的“手动显示”(如所述此处)。
有没有办法在循环内显示文本表单,无论是在第 x 个循环之后还是在特定表单字段之后?
感谢您的建议!
Hi Stackoverflow people,
I am displaying a large form through a loop:
<table>
{% for field in projectDetailForm %}
<tr>
<td> {{ field.label_tag }} </td>
<td> {{ field }} </td>
</tr>
{% endfor %}
</table>
I would like to interrupt the table with the form fields after a few form fields to display more explanations. Since the form is fairly large (20 fields), I would like to avoid the "manual display" of each form field (as described here).
Is there a way to display the text form within the loop, either after the x-th loop or after a specific form field?
Thank you for your advice!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会使用
forloop.counter
或在表单初始化时在表单字段上设置自定义属性,并以与显示field.label_tag
相同的方式显示该属性I would either use
forloop.counter
or set a custom attribute on a form field on form initialization and display the attribute the same way you displayfield.label_tag
您可以向表单添加一个方法,该方法将按部分返回字段。像这样:
然后在模板中:
You can add a method to your form, which will return fields by portions. Something like:
Then in template:
您可以使用
{{ forloop.counter }}
和{{ forloop.counter0 }}
(分别为 1 索引和 0 索引)。有关更多信息,请查看此 Djangobook 链接。
There is a
{{ forloop.counter }}
and{{ forloop.counter0 }}
(1-indexed and 0-indexed respectively) that you can use.For a few more information, check this Djangobook link.