Django: templates, how to fill a table of 3 elements in a row

I want to populate the table with a list of results. But I’m not sure how to tell the template that it closes the current time and starts a new one, after 3 products have already been displayed in the current tr ....

I would be glad if someone could consult with him. I am running django 1.1.1

+4
source share
2 answers

The easiest way is to use a divisibleby filter.

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

Even better, add the condition foorloop.last add </tr> at the end of the last line:

 {% 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 %} 
+12
source

All Articles