When I try and access a simple dict in django:
{'The Batman': [{'datetime': datetime.datetime(2022, 4, 1, 11, 0, tzinfo=datetime.timezone.utc), 'id': '1'},{'datetime': datetime.datetime(2022, 4, 1, 11, 0, tzinfo=datetime.timezone.utc), 'id': '1'}],
'Ice Age': [{'datetime': datetime.datetime(2022, 4, 1, 11, 0, tzinfo=datetime.timezone.utc), 'id': '1'}, {'datetime': datetime.datetime(2022, 4, 1, 11, 0, tzinfo=datetime.timezone.utc), 'id': '1'}]}
I use this loop to access the keys:
{% for film in data %}
...Code here
{% endfor %}
but then when I go to access the values, it returns nothing. I am accessing the values as follows:
{% for showing in data.film %}
...Code here
{% endfor %}
I am printing out the data to my screen every time, so I know there is data there, because the headings register too. Unsure what is happening or what I am doing wrong.
Fairly new to Django so pls be nice :)
CodePudding user response:
You'll need to use .items
to loop over a dict, almost just like you'd do in plain Python code:
{% for film, showings in data.items %}
<h1>{{ film }}</h1>
<ul>
{% for showing in showings %}
<li>{{ showing.datetime }}</li>
{% endfor %}
</ul>
{% endfor %}
CodePudding user response:
Data is a dictionary so you need to run over it’s items.
Like this:
{% for key, values in data.items %}