5

我想在我的模板上显示一个列表。所以我有一个视图来生成该列表并将其传递给模板,如下所示:

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

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

这是模板代码:

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

我不明白这个错误。此外,如果这种方法是错误的(我认为是),那么我将如何将列表传递给模板?

4

2 回答 2

15

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

于 2012-10-06T15:45:22.243 回答
5

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

<ul>
  {% for link in links %}
    <li>{{ link }}</li>
  {% endfor %} 
</ul>
于 2013-10-05T17:16:38.850 回答