我想要做的事很简单:我有一个模型,我正在运行一个重组循环。现在我想要显示循环的前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中进行分组”。 –