如何在Django模板中解析这些数据?

时间:2016-07-13 20:40:34

标签: python django

{u'4th-of-July': {'name': 4th of July', 'slug': u'4th-of-July'}}

我希望能够在django HTML模板中访问'name''slug'。我该怎么做呢?

2 个答案:

答案 0 :(得分:0)

您可以使用点表示法访问模板中的dict:

让我们调用你的字典data

{{ data.4th-of-July.name }}

{{ data.4th-of-July.slug }}

答案 1 :(得分:0)

理想情况下,在这些情况下,您应该迭代dict的键。这是一个例子:

{% 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 %}