在django模板中增加一个变量 [英] increment a variable in django templates
本文介绍了在django模板中增加一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
全部
如何在django模板中增加如下所示的值,
How Can we increment a value like the following in django templates,
{{ flag =0 }}
{% for op in options %}
{{op.choices}}<input type="radio" name="template" id="template" value="template{{flag++}}"/>
{% endfor %}
谢谢..
推荐答案
我不认为你应该改变你的模板中的数据。因为在具体情况下,您可以使用 forloop.counter
变量。
I don't think it's intended you should alter data in your templates. For in your specific case, you could instead use the forloop.counter
variable.
例如:
For example:
{% for op in options %}
{{op.choices}}<input type="radio" name="template" id="template{{forloop.counter}}" value="template{{forloop.counter}}"/>
{% endfor %}
另请注意,我将该号码添加到 id
属性的< input />
标签。否则你会有多个输入与相同的id。
Also note that I added that number to the id
attributes of the <input />
tag. Otherwise you'll have multiple inputs with the same id.
编辑:我没有注意到这是一个无线电输入。您当然可以为每个< input type =radio/>
具有相同的名称。
I didn't note that it was a radio input. You could of course have the same name for each <input type="radio" />
.
这篇关于在django模板中增加一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文