我在模板中有以下数据
{u'D - content a': [<Person: first_name last_name>, <Person: first_name last_name>, <Person: first_name last_name>],
u'D - contant b': [<Person: first_name last_name>],
u'D - content c': [<Person: first_name last_name>]
}
我希望这样:
h2 -> Content a
list with three people
h2 -> content b
list with one person
h3 -> content c
list with one person
但是我不知道两个人是怎么写的。我的尝试:
{% for key, value in persons %}
<h2>{{ key }}</h2>
{{ value }}
{% endfor %}
答案 0 :(得分:2)
使用dict.items
(如果使用Python 2.x,则使用dict.iteritems
):
{% for item in persons.items %}
<h2>{{ item.0 }}</h2>
{{ item.1 }}
{% endfor %}