对于Django中的不同行为也是如此

时间:2014-03-31 20:09:56

标签: html django templates for-loop

我有一个小问题,它是关于Django模板中的循环:

我想做一个for和在每次迭代中做一些不同的事情,例如

{% for object in objects %}

##### html code to put a div with object information in the left and one in the right ######

{% endfor %}

打印我的所有对象,但左边一个,右边另一个。

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:3)

您需要cycle tag

{% for object in objects %}
  <li class="{% cycle "your-left-class" "your-right-class" %}">
    {{object}}
  </li>
{% endfor %}

答案 1 :(得分:-1)

{% for object in objects %}
   <div class="
      {% if forloop.counter % 2 == 0 %}
         pull-left
      {% else %}
         pull-right
     {% endif %}
   ">
      {{object}}
   </div>
{% endfor %}

检查模板中的for loop

相关问题