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
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 %}
Note that instead of using
{% autoescape off %}
{{ my_json }}
{% endautoescape %}
You can simply use a filter :
{{ my_json|safe }}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With