django模板中的模数/模数等效运算符/函数?

Dav*_*vid 9 django templating operators

我只是在学习django的模板系统并尝试做一些相对微不足道的事情:

<h2>State</h2>
<ul class="states">
{% for state in states %}
   <li class="state_elements" ><a href="/{{ state.name }}/"> {{ state.name }}</a></li>
   {% if forloop.counter \% 3 == 0 %}
   <br style="clear: both"/>
 {% endif %}
{% endfor %}
</ul>
Run Code Online (Sandbox Code Playgroud)

我收到语法错误,因为%是为模板语言保留的符号.这很不幸.

我已经找到了部分解决方案

{% cycle "" "" "" '<br style="clear: both"/>' %}
Run Code Online (Sandbox Code Playgroud)

但它让我感到非常奇怪.有没有更好的办法?

iMo*_*om0 26

divisibleby

如果值可以被参数整除,则返回True.

例如:

{{ value|divisibleby:"3" }}
Run Code Online (Sandbox Code Playgroud)

django模板文档

  • {%if forloop.counter | divisibleby:"3"%} (8认同)