Reputation: 943
{u'4th-of-July': {'name': 4th of July', 'slug': u'4th-of-July'}}
I would like to have access to 'name'
and 'slug'
in a django HTML template. How do I go about doing this?
Upvotes: 1
Views: 572
Reputation: 971
Ideally in these cases you should iterate over the keys of the dict. Here is an example:
{% for key, value in dict_name.items %} <!-- where dict_name denotes name of the dictionary containing {u'4th-of-July': {'name': 4th of July', 'slug': u'4th-of-July'}} -->
{{key}} <!-- '4th-of-July' for the index 4th-of-July -->
{{value.name}} <!-- equivalent to dict_name.4th-of-July.name for the index 4th-of-July -->
{{value.slug}} <!-- equivalent to dict_name.4th-of-July.slug for the index 4th-of-July -->
{% endfor %}
Upvotes: 0
Reputation: 10256
You can access a dict in template with dot notation:
Let's call your dictionary data
:
{{ data.4th-of-July.name }}
And
{{ data.4th-of-July.slug }}
Upvotes: 1