python - Django: How to iterate over two lists inside template -


this question has answer here:

from views.py send template 2 array:

  1. animal: ['cat','dog','mause']
  2. how_many: ['one','two','three']

template:

{% value in animal %}     animal:{{ value }}  \ how meny  {{ how_many[(forloop.counter0)]  }} {% endfor %}  

in loop want read iteration , use in second array, can't work. i'm beginner.

according docs, you can't straightforward:

note “bar” in template expression {{ foo.bar }} interpreted literal string , not using value of variable “bar”, if 1 exists in template context.

i suggest add zip(animal, how_many) template's context:

context['animals_data'] = zip(animal, how_many) 

then can access both lists:

{% animal, how_many in animals_data %}     {{ animal }} {{ how_many }} {% endfor %} 

Comments