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}}');
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}}');
Share
Improve this question
edited Dec 5, 2013 at 10:10
asked Dec 5, 2013 at 10:03
user1781186user1781186
4
- So maybe you should show the template? – Daniel Roseman Commented Dec 5, 2013 at 10:07
- Also, why would you bother deserializing from JSON only to serialize it again immediately? What's the point? – Daniel Roseman Commented Dec 5, 2013 at 10:08
-
4
use
{{myJson|safe}}
– Armance Commented Dec 5, 2013 at 10:08 - @DanielRoseman I added the snippet from my template. – user1781186 Commented Dec 5, 2013 at 10:10
1 Answer
Reset to default 19As Armance says in the ments, you need to mark the variable as safe in the template so it is not escaped:
{{ myJson|safe }}