Iterating through two lists in Django templates

前端 未结 7 2249
甜味超标
甜味超标 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:14

    Simply define zip as a template filter:

    @register.filter(name='zip')
    def zip_lists(a, b):
      return zip(a, b)
    

    Then, in your template:

    {%for a, b in first_list|zip:second_list %}
      {{a}}
      {{b}}
    {%endfor%}
    

提交回复
热议问题