[Answer]-Django Template – How to Iterate and access a dictionary?

1👍

If your’re user itertools.groupby, pay attention to the note in the docs pages:

The returned group is itself an iterator that shares the underlying iterable with groupby(). Because the source is shared, when the groupby() object is advanced, the previous group is no longer visible. So, if that data is needed later, it should be stored as a list:

groups = []
uniquekeys = []
data = sorted(data, key=keyfunc)
for k, g in groupby(data, keyfunc):
    groups.append(list(g))      # Store group iterator as a list
    uniquekeys.append(k)

(Bolds added)

Don’t use dict as a varname, and when creating this dict, convert the _grouper values to list. Nothing seems wrong with your template.

0👍

{% for item in dict%}
//do stuff
{% endfor %}

should work unless you dict has a nested attribute “items” that you want to iterate through.

Leave a comment