开发者

django: templates how to fill a table of 3 items in a row

I want to fill a table with a list of results. But开发者_Python百科 not sure how to tell the template that it's time to close current and start a new one, after 3 products was already displayed in current tr....

Would be happy if somebody could advice on it. I am running django 1.1.1


Even better is to add a foorloop.last condition to add </tr> at the end of the last row:

{% for item in results %}
{% if forloop.counter0|divisibleby:3 %}<tr>{% endif %}
<td>{{ item }}</td>
{% if forloop.counter|divisibleby:3 or forloop.last %}</tr>{% endif %}
{% endfor %}


Easiest way is to use the divisibleby filter.

{% for item in results %}
{% if forloop.counter0|divisibleby:3 %}<tr>{% endif %}
<td>{{ item }}</td>
{% if forloop.counter|divisibleby:3 %}</tr>{% endif %}
{% endfor %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜