我正在尝试按照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
对你有意义吗?我准备打碎我的键盘。
答案 0 :(得分:1)
使用adictionary.items
访问key, val
{% for key, value in adictionary.items %}
{{ key }} : {{ value }} <br/>
{% endfor %}