Iterating through two lists in Django templates

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

    In views.py:

    foo = ['foo', 'bar']
    moo = ['moo', 'loo']
    zipped_list = zip(foo,moo)
    return render(request,"template.html",{"context":zipped_list}
    

    In template.html:

    {% for f,m in context%}
     {{f}}{{m}}
    {% endfor %}
    

    If f is a queryset returned from database then access it by {{f.required_attribute_name}}

提交回复
热议问题