My view code looks basically like this:
context = Context()
context[\'my_dict\'] = {\'a\': 4, \'b\': 8, \'c\': 15, \'d\': 16, \'e\': 23, \'f\': 42 }
context
For my needs, I wanted a single template filter that would work for dicts, lists, and tuples. So, here's what I use:
@register.filter
def get_item(container, key):
if type(container) is dict:
return container.get(key)
elif type(container) in (list, tuple):
return container[key] if len(container) > key else None
return None