在模板中显示字典值

时间:2011-10-20 15:16:06

标签: django django-templates

嗨,我有一个非常基本的问题。 我有如下观点:

def view1:
    dict = ('one':'itemone','two':'itemtwo','three','itemthree')
    return render_to_response('test.html',dict)

的test.html

<body>
{% for key,value in dict.items %}{{ value }}{% endfor %}
</body> 

它不起作用。任何人都可以建议正确的方法来迭代模板中的字典值。提前致谢。再次对我的基本问题感到抱歉。

2 个答案:

答案 0 :(得分:13)

我想你想要

def view1:
   d = {'one':'itemone', 'two':'itemtwo', 'three':'itemthree'}
   return render_to_response('test.html', {'d':d})

<body>
 {% for key,value in d.items %}{{ value }}{% endfor %}
</body> 

答案 1 :(得分:2)

您的字典语法已关闭。试试这个:

my_dict = {'one':'itemone','two':'itemtwo','three':'itemthree'}

(我称之为dict以外的其他内容,因此您不会覆盖python的dict类型。)