In Django, I have a view that requests a JSON feed and renders the response along with a template. I need to convert the JSON object to a Javascript JSON object. I have managed to console.log the JSON object in my template, but something is wrong with the format.
This is what I expect:
{"lat": 58.548703, "referenceTime": "2013-12-05T07:00:00Z", "lon": 16.155116...
and this is what I get:
{"lat": 58.548703, "referenceTime": "2013-12-05T07:00:00Z", "lon": 16.155116...
Here is my view:
def myView(request):
request = Request('http://somedomain/somefeed.json')
response = urlopen(request)
data = json.load(response)
return render_to_response('myTemplate.html', {'myJson': json.dumps(data)})
And in my template:
console.log('{{myJson}}');
正如Armance在评论中所说,您需要在模板中将变量标记为安全,以便它不会被转义:
{{ myJson|safe }}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.