[Python] Modulus % in Django template

Discussão em 'Python' iniciado por Stack, Abril 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm looking for a way to use something like the modulus operator in django. What I am trying to do is to add a classname to every fourth element in a loop.

    With modulus it would look like this:

    {% for p in posts %}
    <div class="post width1 height2 column {% if forloop.counter0 % 4 == 0 %}first{% endif %}}">
    <div class="preview">

    </div>
    <div class="overlay">

    </div>
    <h2>p.title</h2>
    </div>
    {% endfor %}


    Of course this doesn't work because % is a reserved character. Is there any other way to do this?

    Continue reading...

Compartilhe esta Página