Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a python list to django template

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?

like image 343
karambir Avatar asked Oct 06 '12 15:10

karambir


1 Answers

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

like image 192
Garrett Hyde Avatar answered Oct 02 '22 17:10

Garrett Hyde