How to use Django template custom variable, like forloop.counter and use to judge

时间:2015-07-28 15:50:31

标签: django

I want to set a auto_increment variable for some judge,but I found

forloop.counter

can't be use like this

<ul class="listcont2">
{% for item in lists%}
{%if forloop.counter%6==0 && forloop.counter<12 %}
    <li><a  ><img  /></a></li>
</ul>
<ul class="listcont2">
{%else%}
    <li><a  ><img  /></a></li>
{%endif%}
{%endfor%}
</ul>

Is there anyway to set a custom auto_increment variable ?

1 个答案:

答案 0 :(得分:2)

You can use divisibleby instead of the modulus, and and instead of &&:

{% if forloop.counter|divisibleby:"6" and forloop.counter < 12 %}