2011-11-05 20 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

相關問題