Django模板中的循环行为很奇怪

时间:2012-12-24 13:27:33

标签: django dictionary for-loop django-templates

我正在尝试按照here所述在我的模板中编写for循环。

views.py:

def simple_view(self, request):
    adictionary = {'first': 'this is the first value', 
                   'second': 'this is the second value', 
                   'third': 'this is the third value'}
    return render_to_response("index.html", 
                            {'adictionary': adictionary}, 
                            context_instance=RequestContext(request))

的index.html:

{% for key, value in adictionary %}
    {{ key }} : {{ value }} <br/>
{% endfor %}

这是我对HTML输出的期望:

first : this is the first value
second : this is the second value
third : this is the third value

这就是我得到的:

s : e 
t : h 
f : i 

对你有意义吗?我准备打碎我的键盘。

1 个答案:

答案 0 :(得分:1)

使用adictionary.items访问key, val

{% for key, value in adictionary.items %}
    {{ key }} : {{ value }} <br/>
{% endfor %}
相关问题