In my Django/Python application I am passing multiple iterable objects from views.py to index.html. I am essentially trying to make a standard for loop in which you can get an item by its index. Once I am iterating through the first object, how can I get the respective index of another iterable in that same loop?
Conditions: All of the arrays will always be equal in length, but their length will change on a daily basis.
views.py
name = ['John', 'Kim', 'Tim', 'Jill']
age = ['20', '30', '52', '27']
state = ['IN', 'IL', 'CA', 'TX']
city = ['Muncie', 'Champaign', 'Fresno', 'Dallas']
def index(request):
args = {'names': name, 'ages': age, 'states': state, 'cities': city}
return render(request, 'index.html', args)
index.html
{% extends 'base.html' %}
{% load staticfiles %}
{% block Data %}
{% for name in names %}
<p>{{ name }}</p>
<p>{{ age.forloop.counter0 }}</p>
<p>{{ state.forloop.counter0 }}</p>
<p>{{ city.forloop.counter0 }}</p>
{% endfor %}
{% endblock %}
As you can see, I thought I would use 'forloop.counter0' as my index. But it doesn't work that way apparently. Any suggestions on how to achieve this goal? Thanks in advance!