My view computes a json and outputs a json.dumps(), and I'm passing this as the dictionary key data. I'm trying to pass this to a script element in my template, but when rendering, the browser gets it as a python-escaped string{"nodes": [{"count":...... which isn't readable to the javascript. What I need is python to send it as a JS-escaped string, something like this {"nodes": [{"count":.......
I tried str(data) and eval(data) without success. Basically I need python to send the string just as if it were printing it to the console. Thanks
-
is this question of any help to you? stackoverflow.com/q/1445989/92493Otto Allmendinger– Otto Allmendinger2012-07-19 19:57:47 +00:00Commented Jul 19, 2012 at 19:57
-
I believe that if you're rendering it to your HTML template and that this HTML template will be send to the browser using the mime type 'text/html' (or it's variant) than the browser will escape the quotes and such, so i think it really depend on the mime type that you're sending to the browser.mouad– mouad2012-07-19 20:00:43 +00:00Commented Jul 19, 2012 at 20:00
Add a comment
|
3 Answers
This works for me:
return HttpResponse(json.dumps({'foo' : 'bar'}, ensure_ascii=False),
mimetype='application/json')
2 Comments
leonsas
I use something like this when sending a simple HttpResponse, but in this case I had to render a template.
Brandon Taylor
Np. Glad the answer was a simple one.