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 %}
source share