[Answer]-Django create multi colums flexible best practices

1πŸ‘

βœ…

This is a quick example on how you can do this (this is for 2 column based itteration) but you can change it to your liking (replace β€œ2” with a integer for instance):

<div class="row">
{% for image in image_list %}
    <div class="col-sm-6">
        <a href="{{ image.url }}">{{ image.name }}</a>
    </div>
    {% if not forloop.last and forloop.counter|divisibleby:"2" %}
</div>
<div class="row">
{% endfor %}
</div>
πŸ‘€petkostas

0πŸ‘

In Django Templates you can not use += 1 operator
you can use the forloop.counter

    {% if forloop.counter0|divisibleby:4 %}   so every 4 iterations 
                                            starting with first iteration where 
                                            counter is 0
        <div class="row">
    {% endif %}
        <div class="col-sm-{{ taille }}">
            <a href="{{ image.url }}">{{ image.name }}</a>
        </div>
    {% if forloop.counter0|add:"-1"|divisibleby:"4" %}  also every 4 iterations 
                                                       but starting with the 2nd
                                                       iteration where counter is 1
        </div>
    {% endif %}
πŸ‘€gaw

Leave a comment