[Fixed]-How to break down a Django query-set in a template and then iterate?

1👍

You can use a forloop.counter and divisibleby to accomplish this:

<div>
    {% for post in posts %}
        <p>{{post.text}}</p>
        {% if forloop.counter|divisibleby:3 %}
            </div><div>
        {% endfor %}
    {% endfor %}
</div>

Alternatively, consider splitting the queryset into groups in the view and passing the groups to the template as a new context variable.

Leave a comment