将python列表传递给Django模板 [英] Passing a python list to django template
本文介绍了将python列表传递给Django模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在模板上显示事物列表。因此,我有一个视图来生成该列表并将其传递给模板,如下所示:
I want to display a list of things on my template. So I have a view to generate that list and pass it to template like this:
newlinks = []
try:
links=urllib2.urlopen("<<Some HTML file link>>").readlines()
except (urllib2.HTTPError):
links = ''
pass
for link in links:
newlinks.append(link[0:-1])
return render_to_response('template11.html', {'links',newlinks}, context_instance=RequestContext(request))
但是在渲染它时,我得到TypeError
But while rendering it, i get TypeError
Exception Type: TypeError
Exception Value: unhashable type: 'list'
这是模板代码:
{% for link in links %}
<li>{{ link }}</li>
{% endfor %}
我不了解此错误。另外,如果这种方法是错误的(我认为是这样),那么如何将列表传递给模板?
I don't understand this error. Also if this approach is wrong(I think it is), then how would I pass a list to template?
推荐答案
在 return render_to_response()
, {'links',newlinks}
导致错误。应该是 {’links':newlinks}
。
In return render_to_response()
, {'links',newlinks}
is causing the error. It should be {'links': newlinks}
.
这篇关于将python列表传递给Django模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文