【问题标题】:How to get the half part of forloop in django template如何在 django 模板中获取 forloop 的一半
【发布时间】:2012-03-23 21:16:18
【问题描述】:

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

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

谢谢

【问题讨论】:

    标签: python html django


    【解决方案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:]
    

    【讨论】:

    • 谢谢,我知道了,我可以在views.py中过滤它们。
    • NP,请关闭问题:)
    • 无需关闭问题。他应该发布自己的答案!
    猜你喜欢
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-02
    • 2013-09-07
    • 1970-01-01
    • 2011-07-07
    • 2019-12-05
    相关资源
    最近更新 更多