Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript-readable json from python

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

like image 546
leonsas Avatar asked Jul 19 '12 19:07

leonsas


Video Answer


2 Answers

If I understand well, you want to use a json in a template. In order to do that, you have to disable the escaping, for exemple like this.

{% autoescape off %}
var x={{json_var}}
{% endautoescape %}
like image 120
kevin Avatar answered Oct 10 '22 10:10

kevin


Note that instead of using

{% autoescape off %}
    {{ my_json }}
{% endautoescape %}

You can simply use a filter :

{{ my_json|safe }}
like image 38
Byscripts Avatar answered Oct 10 '22 11:10

Byscripts