2011-11-05 65 views
1

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

这个问题与Python没有任何关系。 –

+0

@DanielRoseman不同意。该应用程序是用Python编写的,它使用了Python模板引擎,该解决方案可能是“在传递给模板之前在Python中进行分组”。 –

回答

1
<table cellspacing="0"> 
{% for item in in_country.list|dictsort:"name" %} 

{% if forloop.counter0|divisibleby:4 %} 
<tr style="text-align:center;"> 
{% endif %} 

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

{% if forloop.counter|divisibleby:4 %} 
</tr> 
{% endif %} 

{% endfor %} 
</table> 
+1

谢谢你的回答。我明白你想要做什么,实际上比我的尝试更合乎逻辑。但是尝试的代码时,它提供了一个错误: TemplateSyntaxError:无效的过滤器:“divisible_by” 顺便说一句,应该不是“forloop.counter”末还以0(forloop.counter0)结束 – Vincent

+1

对不起,应该是['divisibleby'](https://docs.djangoproject.com/en/1.3/topics/signals/#connecting-to-signals-sent-by-specific-senders)。不,你希望第一个标签只出现在第0,4,8行等,但最后一个出现在第3,7,11行等。 –

+1

我想到了它:)它是divisibleby而不是divisible_by。剩下的我就放弃了。非常感谢你! PS:在写完上述内容后阅读您的评论 – Vincent

0

我建议把它传递给模板之前,在你的Python代码分组你行。你可以使用迭代器这样做:

my_iter = iter(my_list) 
grouped = zip(my_iter, my_iter, my_iter, my_iter) # or zip(*[my_iter]*4) 
+0

感谢Nick。在发布问题之前,我研究了iter函数,但我不明白这个问题。猜猜我会回到那个文档:)再次感谢! – Vincent