Django dictionary object shows nothing in template but prints to console -
following django's tutorials https://docs.djangoproject.com/en/1.6/topics/db/sql/#executing-custom-sql-directly have called stored procedure , populated dictionary object.
views.py
@login_required def team_edit(request): user_instance = request.user user_id = user_instance.id cursor = connection.cursor() cursor.execute("call test(%s)", user_id) #call db stored procedure results = dictfetchall(cursor) print(results) homecoming render_to_response('team_edit.html',results, context_instance=requestcontext(request)) # converts list dict def dictfetchall(cursor): "returns rows cursor dict" desc = cursor.description homecoming [ dict(zip([col[0] col in desc], row)) row in cursor.fetchall() ]
printing print(results)
shows in console:
[{'private_league_name': "root's league", 'host_user_id': 1}, {'private_league_name': "joe's league", 'host_user_id': 3}]
however list in template shows nothing:
<h2>results test</h2><br> <ul> {% key, value in results.items %} <li> {{ key }}: {{ value }}</li> {% endfor %} </ul>
why this?
you misunderstanding dictionary used when calling render_to_response
. if phone call
return render_to_response('team_edit.html',{'customer_id' : 5}, context_instance=requestcontext(request))
i able use
{{customer_id}}
somewhere in template.
in case, want
return render_to_response('team_edit.html',{'results' : results}, context_instance=requestcontext(request))
however, notice results
variable list, not dictionary, in template need like
{% result in results %} {% key, value in result %} ...
django django-templates django-views
No comments:
Post a Comment