【发布时间】:2011-12-22 18:19:14
【问题描述】:
我想做的很简单:我有一个模型,我正在运行一个重组循环。现在我想将循环的前 4 项并排显示,然后在下一行继续显示接下来的 4 项,直到列表完成。如果没有更多项目,则该行中的其他单元格可以保持空白。
我试图了解如何在这里使用迭代,但无法弄清楚它如何适合我所拥有的。另外,我认为应该可以以更简单的方式执行此操作...下面我复制了我现在拥有的代码,这显然显示了循环的第一项 4 次,然后是第二项的 4 次,等等。
我知道这个网站上有一些类似的问题,但我无法利用它们为我的问题开发解决方案。我在 Google App Engine 上运行 Python。非常感谢任何帮助!
{% regroup communities|dictsort:"in_country" by in_country as community_list %}
{% for in_country in community_list %}
<h2 style="font-size:16px;">{{ in_country.grouper }}</h2>
<table cellspacing="0">
{% for item in in_country.list|dictsort:"name" %}
<tr style="text-align:center;">
<td width="200px" class='community_table'>
<img src="{{ item.image }}" style="height:40px;"><br />
<a href='{{ item.url }}' style="font-size:10px; margin-left:10px;" TARGET = "_blank">{{ item.name }}</a><br />
{{ item.com_type }}<br />
{{ item.in_city }}<br />
</td>
<td width="200px" class='community_table'>
<img src="{{ item.image }}" style="height:40px;"><br />
<a href='{{ item.url }}' style="font-size:10px; margin-left:10px;" TARGET = "_blank">{{ item.name }}</a><br />
{{ item.com_type }}<br />
{{ item.in_city }}<br />
</td>
<td width="200px" class='community_table'>
<img src="{{ item.image }}" style="height:40px;"><br />
<a href='{{ item.url }}' style="font-size:10px; margin-left:10px;" TARGET = "_blank">{{ item.name }}</a><br />
{{ item.com_type }}<br />
{{ item.in_city }}<br />
</td>
<td width="200px" class='community_table'>
<img src="{{ item.image }}" style="height:40px;"><br />
<a href='{{ item.url }}' style="font-size:10px; margin-left:10px;" TARGET = "_blank">{{ item.name }}</a><br />
{{ item.com_type }}<br />
{{ item.in_city }}<br />
</td>
{% endfor %}
</table>
{% endfor %}
【问题讨论】:
-
这个问题与Python无关。
-
@DanielRoseman 不同意。该应用程序是用 Python 编写的,它使用 Python 模板引擎,解决方案可能是“在传递给模板之前在 Python 中进行分组”。
标签: python google-app-engine loops django-templates