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

前端 未结 7 1173
广开言路
广开言路 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:05

    There's a way to do it server-side with an iterator that doesn't keep a simultaneous copy of all the entries:

    import itertools
    return render_to_response('template.html',
      {
        "flattened_entries": itertools.chain(*(blog.entries for blog in blogs)),
      })
    

提交回复
热议问题