'How do I read a nested dictionary in a Django template?
I am trying to access the following data structure inside a template in Django. But its too difficult for me too figure out how.
{ Day1 : { Room1 : [(datetime.date(), (totalTime1, Flag1)),
(datetime.date(), (totalTime2, Flag2)),
(datetime.date(), (totalTime3, Flag3)),
(datetime.date(), (totalTime4, Flag4))],
Room2 : [(datetime.date(), (totalTime1, Flag1)),
(datetime.date(), (totalTime2, Flag2)),
(datetime.date(), (totalTime3, Flag3)),
(datetime.date(), (totalTime4, Flag4))],
Room3 : [(datetime.date(), (totalTime1, Flag1)),
(datetime.date(), (totalTime2, Flag2)),
(datetime.date(), (totalTime3, Flag3)),
(datetime.date(), (totalTime4, Flag4))] },
Day2 : { Room1 : [(datetime.date(), (totalTime1, Flag1)),
(datetime.date(), (totalTime2, Flag2)),
(datetime.date(), (totalTime3, Flag3)),
(datetime.date(), (totalTime4, Flag4))],
Room2 : [(datetime.date(), (totalTime1, Flag1)),
(datetime.date(), (totalTime2, Flag2)),
(datetime.date(), (totalTime3, Flag3)),
(datetime.date(), (totalTime4, Flag4))],
Room3 : [(datetime.date(), (totalTime1, Flag1)),
(datetime.date(), (totalTime2, Flag2)),
(datetime.date(), (totalTime3, Flag3)),
(datetime.date(), (totalTime4, Flag4))] } }
Probably its a very complex data structure, but I am sorry about it. I couldnt break it up. My choice is limited. I want to access each element in a single loop. How may I do it?
Edit : I want to access the elements from a django template.
Solution 1:[1]
It can't be done in a single loop, but you can do it with some nesting:
{% for day, rooms in mydict.items %}
{% for room, dates_and_flags in rooms.items %}
{% for date, time_and_flags in dates_and_flags %}
{{ date }}:{{ time_and_flags }}
{% endfor %}
{% endfor %}
{% endfor %}
Because time_and_flags is a tuple, you can access the time using time_and_flags.0 and the flags with time_and_flags.1.
Solution 2:[2]
I was able to do it with a single loop - although maybe I misunderstood the previous answers or initial question. Regardless, this might be helpful to anyone else looking into working with nested dictionaries in Django
Combine
- the .items() method to loop over the dictionary in Django
- the .value syntax for the attribute lookup
Your example dictionary wasn't quite a dictionary, so as another example
grades_dict = {'Math': {'grade': 78, 'grade_date': '2016-05-15'},
'Reading': {'grade': 83, 'grade_date': '2016-08-16'},
'Science': {'grade': 98, 'grade_date': '2016-08-16'},
'Social Studies': {'grade': 62, 'grade_date': '2016-05-15'}}
in django template html
{% for key, value in grades_dict.items %}
<p>Grade for {{key}}: {{value.grade}}. </p>
<p>Last entered on {{value.most_recent_grade_date}}</p>
{% endfor %}
results in
Grade for Social Studies: 62.
Last entered on 2016-05-15
Grade for Science: 98.
Last entered on 2016-08-16
Grade for Reading: 83.
Last entered on 2016-08-16
Grade for Math: 78.
Last entered on 2016-05-15
Solution 3:[3]
This is a generator function that will return each row in a loop:
def next_row(data):
for day,rooms in data.iteritems():
for room,times in rooms.iteritems():
for time in times:
yield (day, room, time)
Use it like this:
for row in next_row(data):
# do something with row
Solution 4:[4]
Your question doesn't exactly say what do you want to do with your nested dictionnary, but here's a wild guess:
for day,rooms in days.iteritems():
print "Rooms for day {}:".format(day)
for room, times in rooms.iteritems():
print " . Room {}".format(room)
for t in times:
print "Date : {} | Total time : {} | Flag : {}".format(t[0], t[1][0], t[1][1])
Solution 5:[5]
If you want a certain value, you can access it like this.
{{ Day1.Room1 }}
Check this link https://docs.djangoproject.com/en/4.0/ref/templates/language/#variables
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | girasquid |
| Solution 2 | Scott Weldon |
| Solution 3 | Brent Washburne |
| Solution 4 | halflings |
| Solution 5 | Dave |
