我的表单中有很多字段,我试图将不同的 css 应用于相邻的表单字段,例如
<li class='thiscolor' >
<field>
</li>
<li class='thatcolor' >
<field>
</li>
如果有类似的方法
{% for field in form %}
**{% if forloop.counter%2 == 0 %}**
<li class='thiscolor'>
{% else%}
<li class='thatcolor'>
{%endif}
{{field}}
</li>
{% endfor %}
forloop.counter ?
非常感谢!
最佳答案
cycle tag专为此类问题而设计:
{% for field in form %}
<li class="{% cycle 'thiscolor' 'thatcolor' %}">{{ field }}</li>
{% endfor %}
关于css - django 模板 forloop.counter 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1219649/