django - Access nested dictionary in javascript -


using django 1.3: django view i'm trying pass dictionary

data = {'class1': {'age': 32,                   'count': 1},          'class2': {'age': 43,                    'count': 5},          'class3': {'age': 32,                      'count': 10} } 

from view

render(request, "home.html", {'data': json.dumps(data)}) 

in javascript , trying dictionary but

<script type="text/javascript">                  var data = "{{ data }}"             </script> 

data coming :

"{&quot;unlimited&quot;: {&quot;price&quot;: 99.99, &quot;limit&quot;: 10000}, &quot;premium&quot;: {&quot;price&quot;: 59.99, &quot;limit&quot;: 10}, &quot;free&quot;: {&quot;price&quot;: 0, &quot;limit&quot;: 1}, &quot;basic&quot;: {&quot;price&quot;: 39.99, &quot;limit&quot;: 5}}" 

have tried safe , escape filter, template error : data not parsed.

how access data in javascript correctly ?

i assume trying return json so:

import json django.http import httpresponse  def some_method(requset):     ...     data = {...}      return httpresponse(json.dumps(data), content_type='application/json') 

as far decoding json refer answer https://stackoverflow.com/a/4935684/785808


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -