{% cycle%} work for nested loops?

I came across an interesting "oversight" in the Django template tag {% cycle %} . This was indicated as an error , but I wonder if there is a workaround for this?

 {% for r1 in range_0_2 %} {% for r2 in range_0_3 %} {{ r1 }}-{{ r2 }}-{{ cycle 'even' 'odd' }} {% endfor %} {% endfor %} 

This gives:

 0-0-even 0-1-odd 0-2-even 1-0-odd 1-1-even 1-2-odd 

It should turn out:

 0-0-even 0-1-odd 0-2-even 1-0-even 1-1-odd 1-2-even 
+7
django django-templates
source share
2 answers

I noticed the same problem in my templates.

You can use a workaround as below:

 {% if forloop.counter|divisibleby:2 %}even{% else %}odd{% endif %} 
+10
source share

I use include for inner loop content

 {% regroup employee_bypos_list by pos as by_pos %} {% for pos_set in by_pos %} <h2>ยซ{{ pos_set.grouper.address }}ยป</h2> {% with pos_set.list as employee_list %} {% include 'website/employee/_staff_by_post.html' %} {% endwith %} {% endfor %} 
+1
source share

All Articles