Django模板每两次迭代跳过一行 [英] Django template skip line every two iteration

查看:119
本文介绍了Django模板每两次迭代跳过一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下html结构:

I have the following html structure:

<div class="row>
    <div class="box"></div>
    <div class="box"></div>
</div>

我正在Django上使用分页功能,每页传递6个项目。

I am using pagination feature on Django to pass on 6 items per page.

我要遍历分页器生成的对象列表,同时用行div包装每个两个框div?

How would I go about iterating over the paginator generated object list while wrapper each two box divs with row div?

推荐答案

您可以使用<模板中的code> forloop.counter

{% for obj in obj_list %}
    {% if forloop.counter0|divisibleby:2 %}
    <div class="row">
    {% endif %}
        <div class="box"></div>
        <div class="box"></div>
    {% if forloop.counter|divisibleby:2 %}
    </div>
    {% endif %}

{% else %}
    Nothing to show
{% endfor %}

如果列表中元素的数量为奇数,则它将没有尾随的 div 。我会让你自己弄清楚这种情况。 (非常简单)

and if there are odd number of elements in the list, then it would not have a trailing div. I will let you figure out that scenario by yourself. (it is pretty simple)

forloop.counter0 可以在此处找到
divisibleby 可以在此处找到

Documentation for the forloop.counter0 can be found here Documentation for divisibleby can be found here

这篇关于Django模板每两次迭代跳过一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆