John Smith
John Smith

Reputation: 943

How do I parse this data in a Django template?

{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

Answers (2)

Utkarsh Kaushik
Utkarsh Kaushik

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

Gocht
Gocht

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

Related Questions