views:

72

answers:

1

I ran into an interesting "oversight" in the Django {% cycle %} template tag. This has been listed as a bug, but I'm wondering if there is a workaround for it?

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

This yields:

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

It should yield:

0-0-even
0-1-odd
0-2-even
1-0-even
1-1-odd
1-2-even
+1  A: 

I have noticed the same problem in my templates.

You can use a workaround like the following:

{% if forloop.counter|divisibleby:2 %}even{% else %}odd{% endif %}
Lance McNearney