I have a dict passed to my template, some of the element names / keys begin with an @ symbol, ie: { '@id': 123, '@name': 'Alex' }
Is there a good / clean way I can access these elements?
I have a dict passed to my template, some of the element names / keys begin with an @ symbol, ie: { '@id': 123, '@name': 'Alex' }
Is there a good / clean way I can access these elements?
A custom template tag is the only way to access dictionary keys with special characters. The answer to this question provides a good example.
For the lazy:
from django import template
register = template.Library()
@register.filter
def dictKeyLookup(the_dict, key):
# Try to fetch from the dict, and if it's not found return an empty string.
return the_dict.get(key, '')
Which you would use like so:
{% dictKeyLookup your_dict "@blarg!#$^&*" %}
As homework, you could also convert this into a simple filter, which would give you a syntax like:
{{ your_dict|getkey:"@blarg!@#$%" }}