如何在Django模板中获取forloop的一半 [英] How to get the half part of forloop in django template

查看:44
本文介绍了如何在Django模板中获取forloop的一半的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在另一列中对forloop的一半进行区分,像这样: http://demo.webdeveloperplus.com/drag-drop-panels/现在,我使用django模板编写了它,但是我不知道将一半分成column2吗?

I want to sperate the half part of forloop in another column, like this:http://demo.webdeveloperplus.com/drag-drop-panels/ Now, I wrote it using django template, However I have no idea to separate the half into column2?

下面是我循环第一列中的所有行

Below is what I loop all line in column1

<div class="column" id="column1">
<ul>
{% for line in lines %}
        <li>{{ line.node }}</li>
{% endfor %}
</ul>
</div>

我想要的是:

<div class="column" id="column1">
half nodes
</div>

<div class="column" id="column2">
the another half nodes
</div>

谢谢

推荐答案

您可以在视图中创建两个上下文变量:

You could make two context variables in your view:

offset = len(lines) / 2

if offset % 2 != 0:
    # ensure that the second col does not contain more than the first one
    offset += 1

lines_col0 = lines[:offset]
lines_col1 = lines[offset:]

这篇关于如何在Django模板中获取forloop的一半的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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