{u'4th-of-July': {'name': 4th of July', 'slug': u'4th-of-July'}}
我希望能够在django HTML模板中访问'name'
和'slug'
。我该怎么做呢?
答案 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 %}