Using a cycle in django

I have a webpage where I loop and use a loop inside a loop.

{% for o in something %}
{% for c in o %}
 <div class="{% cycle 'white' 'black'%}"></div>
{% endfor %}

Now this means that every time inside the loop, the first div tag gets white. But I want to alternate between white and black, i.e. start with white, and then the next time the first div tag with black starts inside the loop. Is it possible to achieve here?

+5
source share
2 answers

About the error you can accept the error . You can try the proposed change to see if it works for you.

If you do not want to try, or it does not work, do this:

{% cycle 'white' 'black' as divcolors %}
{% for o in something %}
    {% for c in o %}
        <div class="{% cycle divcolors %}"></div>
    {% endfor %}
{% endfor %}

, , ( , ).

+4

- ():

{% for o in something %}
 {% for c in o %}
  {% ifchanged forloop.parent.counter %}
   <div class="{% cycle 'white' 'black' %}"></div>
  {% else %}
   <div class="{% cycle 'black' 'white' %}"></div>
  {% endifchanged %}
 {% endfor %}
{% endfor %}
0

All Articles