Passing a python list to django template

北城余情 提交于 2019-11-30 17:18:52

问题


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))

But while rendering it, i get TypeError

Exception Type: TypeError
Exception Value: unhashable type: 'list'

This is template code:

{% 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?


回答1:


In return render_to_response(), {'links',newlinks} is causing the error. It should be {'links': newlinks}.




回答2:


And here is what you would put in the template11.html

<ul>
  {% for link in links %}
    <li>{{ link }}</li>
  {% endfor %} 
</ul>


来源:https://stackoverflow.com/questions/12761173/passing-a-python-list-to-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!