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 :
"{"unlimited": {"price": 99.99, "limit": 10000}, "premium": {"price": 59.99, "limit": 10}, "free": {"price": 0, "limit": 1}, "basic": {"price": 39.99, "limit": 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
Post a Comment