Iterating through two lists in Django templates

前端 未结 7 2280
甜味超标
甜味超标 2020-11-27 04:43

I want to do the below list iteration in django templates:

foo = [\'foo\', \'bar\'];
moo = [\'moo\', \'loo\'];

for (a, b) in zip(foo, moo):
    print a, b
<         


        
7条回答
  •  天涯浪人
    2020-11-27 05:25

    I built django-multiforloop to solve this problem. From the README:

    With django-multiforloop installed, rendering this template

    {% for x in x_list; y in y_list %}
      {{ x }}:{{ y }}
    {% endfor %}
    

    with this context

    context = {
        "x_list": ('one', 1, 'carrot'),
        "y_list": ('two', 2, 'orange')
    }
    

    will output

    one:two
    1:2
    carrot:orange
    

提交回复
热议问题