I need to alternate in the template class container div


 {% for smth in smth_list %} {% if (forloop.counter % 2 != 0) %} <!-- Ошибка в этом месте --> <div class="container container-1"> {{ smth }} </div> {% else %} <div class="container container-2"> {{ smth }} </div> {% endif %} {% endfor %} 

This code gives an error:

Could not parse the remainder: '(forloop.counter' from '(forloop.counter'

    2 answers 2

    Divisibleby filter

     {% if forloop.counter|divisibleby:"2" %} 
    • @breaf, thank you so much ... - Niki-Timofe

    In general, this is usually done a little differently, through the cycle :

     {% for smth in smth_list %} <div class="container {% cycle 'container-1' 'container-2' %}"> {{ smth }} </div> {% endfor %} 

    Each time the cycle tag is encountered, the value from the list of specified ones will alternate and be substituted.

    Or, if you want to not output, but keep it in a variable (say, use the value several times), then:

     {% for smth in smth_list %} {% cycle 'container-1' 'container-2' as smth_container silent %} <div class="container {{ smth_container }}"> {{ smth }} </div> {% endfor %}