Alternate Row Coloring in Django Template with More Than One Set of Rows

前端 未结 7 1170
广开言路
广开言路 2020-12-07 14:37

Django templates offer the builtin tag cycle for alternating between several values at different points in a template (or for loop in a template) but this tag d

7条回答
  •  鱼传尺愫
    2020-12-07 15:13

    You can use tagged cycle and resetcycle (new in Django 1.11) calls (from https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#std:templatetag-resetcycle ):

    {% for blog in blogs %}
      {% cycle 'odd' 'even' as rowcolors silent %}
      {% resetcycle rowcolors %}
      {% for entry in blog.entries %}
        {% cycle rowcolors %}
        
    {{ entry.text }}
    {% endfor %} {% endfor %}

提交回复
热议问题