如何在 Django for 循环模板中获得奇数和偶数值?

我有这个密码

{% for o in some_list %}

现在我想做一些事情,如果我是在一条平行线上。我怎么能做到呢?

35862 次浏览

Https://docs.djangoproject.com/en/dev/ref/templates/builtins/#divisibleby

{% if forloop.counter|divisibleby:2 %}even{% else %}odd{% endif %}

第一阶段:

{% cycle 'odd' 'even' %}

参考文献:

<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>

或者

<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>
<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