Django template can't loop defaultdict
Solution 1:
You can avoid the copy to a new dict by disabling the defaulting feature of defaultdict once you are done inserting new values:
new_data.default_factory = None
Explanation
The template variable resolution algorithm in Django will attempt to resolve new_data.items
as new_data['items']
first, which resolves to an empty list when using defaultdict(list).
To disable the defaulting to an empty list and have Django fail on new_data['items']
then continue the resolution attempts until calling new_data.items()
, the default_factory attribute of defaultdict can be set to None.
Solution 2:
try:
dict(new_data)
and in Python 2 it is better to use iteritems
instead of items
:)