如何在django模板中获取for循环的一半

时间:2012-02-26 14:48:20

标签: python html django

我想将forloop的一半部分用于另一列, 像这样:http://demo.webdeveloperplus.com/drag-drop-panels/ 现在,我用django模板写了它, 但是我不知道将这一半分成第二列?

下面是我在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>

由于

1 个答案:

答案 0 :(得分:1)

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

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