[英]Jinja2 Template - for loop
didn't find another post which has the similar problem, I'm trying to generate some checkboxes with flask and wtforms, at the moment I've got this piece of code: 没有找到另一个有类似问题的帖子,我正在尝试用flask和wtforms生成一些复选框,目前我已经得到了这段代码:
<div class="control-group">
<p><strong>Check the enabled BRI Ports</strong></p>
<label class="checkbox inline">
{{ form.bri1(value=1) }} {{ form.bri1.label }}
</label>
<label class="checkbox inline">
{{ form.bri2(value=1) }} {{ form.bri2.label }}
</label>
<label class="checkbox inline">
{{ form.bri3(value=1) }} {{ form.bri3.label }}
</label>
<label class="checkbox inline">
{{ form.bri4(value=1) }} {{ form.bri4.label }}
</label>
</div>
This works so far, but now I try to do this with a simple for-loop like: 到目前为止,这是有效的,但现在我尝试用一个简单的for循环来做到这一点:
<div class="control-group">
<p><strong>Check the enabled BRI Ports</strong></p>
{% for n in range(1,6) %}
<label class="checkbox inline">
{{ form.brin.label }}
{% endfor %}
</div>
I tried with (), {} and {{}} ... is this even possible? 我试过用(),{}和{{}} ......这有可能吗?
Try: 尝试:
<div class="control-group">
<p><strong>Check the enabled BRI Ports</strong></p>
{% for name, field in form._fields.items() %}
{% if name != 'csrf_token' %}
<label class="checkbox inline">
{{ field(value=1) }} {{ field.label }}
</label>
{% endif %}
{% endfor %}
</div>
There you can set sorting instead form._fields.items()
or condition instead {% if name != 'csrf_token' %}
. 在那里你可以设置排序而不是form._fields.items()
或条件而不是{% if name != 'csrf_token' %}
。 Or: 要么:
<div class="control-group">
<p><strong>Check the enabled BRI Ports</strong></p>
{% for n in range(1,6) %}
{% if form['bri' + n|string] %}
<label class="checkbox inline">
{{ form['bri' + n|string](value=1) }} {{ form['bri' + n|string].label }}
</label>
{% endif %}
{% endfor %}
</div>
There you can also use n.__str__()
instead filter n|string
. 在那里你也可以使用n.__str__()
代替过滤n|string
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.