[Fixed]-How to go through a ManyToMany field in chunks in Django

1👍

{% for photo in album.images.all %}
    {{ photo }}
    {% if forloop.counter|divisibleby:4 %}
        <hr> {# or some other markup #}
    {% endif
{% endfor %}

You might also do the grouping in your view. This might result in cleaner markup. See: Django template {%for%} tag add li every 4th element

Leave a comment