我有这段代码
{% for o in some_list %}
现在我想做一些事情,如果我在一个偶数线上。我怎么能这样做?
答案 0 :(得分:112)
https://docs.djangoproject.com/en/dev/ref/templates/builtins/#divisibleby
来自here:
{% if forloop.counter|divisibleby:2 %}even{% else %}odd{% endif %}
答案 1 :(得分:86)
在第一级循环中:
{% cycle 'odd' 'even' %}
答案 2 :(得分:1)
<div class="row">
{% for post in posts %}
{% cycle 'odd' 'even' %}
{% if cycle == 'odd' %}
<div class="col-md-6">Odd posts</div>
{% else %}
<div class="col-md-6">Even posts</div>
{% endif %}
{% endfor %}
</div>
OR
<div class="row">
{% for post in posts %}
{% if forloop.counter|divisibleby:2 %}
<div class="col-md-6">Even posts</div>
{% else %}
<div class="col-md-6">Odd posts</div>
{% endif %}
{% endfor %}
</div>
答案 3 :(得分:0)
<div class="row">
{% for post in posts %}
{% if loop.index is divisibleby 2 %}
<div class="col-md-6">Even posts</div>
{% else %}
<div class="col-md-6">Odd posts</div>
{% endif %}
{% endfor %}
</div>
http://mitsuhiko.pocoo.org/jinja2docs/html/templates.html#id3