Django:循环迭代表单字段 [英] Django: For Loop to Iterate Form Fields
本文介绍了Django:循环迭代表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不想使用django的内置表单生成,希望在模板中指定每个字段,以便自定义html输出。
I don't want to use django's built in form generation, seeking to specify each field in my template in order to customize the html output.
如何迭代一系列表单域?
How do I iterate over a series of form fields?
如果我的表单如下所示: p>
If my form looks like this:
class MyForm(forms.Form):
main_image = forms.ImageField()
second_image = forms.ImageField()
third_image = forms.ImageField()
fourth_image = forms.ImageField()
...
有没有办法为%} 循环编写一个 {%},以便我可以遍历:
Is there way to write a {% for %}
loop so that I can iterate through:
{{ form.main_image }}
{{ form.second_image }}
{{ form.third_image }}
{{ form.fourth_image }}
我尝试了以下这似乎是合乎逻辑的,但没有起作用: p>
I tried the following which seemed logical, but did not work:
{% for field in form %}
{{ form.field }}
{% endfor %}
推荐答案
这显然不起作用:
{% for field in form %}
{{ form.field }}
{% endfor %}
但这将:
{% for field in form %}
{{ field }}
{% endfor %}
这篇关于Django:循环迭代表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文