Skip to content Skip to sidebar Skip to footer

Djago Template Card Alignement [Django 2.2]

I Need your help please. My envirenement is: python 3.6.6 django 2.2 I want to generate some code like this using django template. what I manage to do with django templates is as

Solution 1:

Use the forloop.counter0 and the divisibleby tempate tag like this:

{% for actu in actu %}
    {% if forloop.counter0|divisibleby:"4" and not forloop.first %}
        </div>
    {% endif %}
    {% if forloop.counter0|divisibleby:"4" %}
        <div class="card-deck">
    {% endif %}
    <div class="card">
        {{ actu.content }}
    </div>
    {% if forloop.last %}
        </div>
    {% endif %}
{% endfor %}

Here we first check if the loop counter is divisible by 4 and not the first iteration if yes we render the closing div tag.
Next we if the loop counter is divisible by 4 and if so we render the opening div tag.
After this we render the individual cards.
Next we check if this is the last iteration if so we render the closing div tag.


Solution 2:

{% for item in actu %}
    {% if forloop.counter0|divisableby:4 %}
    <div class="card-deck">
    {% endif %}
        <div class="card">
            {{ item.content }}
        </div>
        {% if forloop.counter0|divisableby:4 or forloop.last %}
    </div>
    {% endif %}
{% endfor %}

Post a Comment for "Djago Template Card Alignement [Django 2.2]"