Iterating through two lists in Django templates

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

    You can use zip in your view:

    mylist = zip(list1, list2)
    context = {
                'mylist': mylist,
            }
    return render(request, 'template.html', context)
    

    and in your template use

    {% for item1, item2 in mylist %}
    

    to iterate through both lists.

    This should work with all version of Django.

提交回复
热议问题