How to iterate over a python dictionary in django template -


i saw variety of answers , can't figure out doing wrong currently. using python 2.7 , django 1.5.1. below code template:

  {% key, value in chartdata.items %}     key = {{ key }};     value = {{ value }};     console.log( key + ":" +  value  );     alert('here');   {% endfor %} 

and in view.py sending dictionary as:

  chartdata= getchartdata(request.session['userphone'])     log.debug(chartdata)     table=getuserinfo(request.session['userphone'],str(request.user))     return render(request,'users.html',{'table':table,'topics':request.session['topics'],'profilepic':request.session['profilepic'],'chartdata':chartdata,'time':str(time.time())}) 

the log.debug(chartdata) returning following result in logfile:

  [11/jul/2013 16:49:12] debug [karnadash.views:179] [(85, '2013-07-08'), (120, '2013-07-08'), (205, '2013-07-08'), (305, '2013-07-08'), (405, '2013-07-08'), (505, '2013-07-08'), (547, '2013-07-09'), (564, '2013-07-09'), (581, '2013-07-09'), (607, '2013-07-09'), (624, '2013-07-09'), (659, '2013-07-09'), (694, '2013-07-09'), (711, '2013-07-09'), (737, '2013-07-09'), (754, '2013-07-09'), (771, '2013-07-09'), (871, '2013-07-09')] 

can please point out wrong in code.

i found following question pretty same mine , tried solution.

what's wrong here? iterating on dictionary in django template

i have tried using: chartdata.iteritems has had similar effect. don't error. never goes in loop. when console.log(chartdata) shows parsing error ('& error something') in browser console within junk data can find dictionary having data. iteritems or items not work.

edit: problem turned out using integers dictionary key

however problem still persists.

 alert({{  value  }}); 

at javascript side. , value 'hello' says client side no variable name hello defined. puts in whole instead of string? should right?

chartdata list of tuples, not dict. need in template:

{% row in chartdata %}     console.log("{{ row.0 }}", "{{ row.1 }}"); {% endfor %} 

Comments