Import Python Flask JSON dictionary into javascript error

I have a Python Flask web application that I want to pass a dictionary dataset from javascript, I'm trying to use JSON. I can pass numbers in order, but it seems to generate an error when using strings.

Here is a snippet of python code generating a JSON string: view.py


def dumpdata(): DB_name={"name":"aaragh"} strng=json.dumps(DB_name) return render_template('dumpdata.html',result = strng) 

Here is the resulting dumpdata.html HTML file


 <html><body> <p >{{ result }}</a> <script> var data = JSON.parse({{result}}); console.log(data.name); </script> </body></html> 

Here is the error message and console output: ConsoleLog


SyntaxError: invalid dumpdata property id: 4

 <html><body> <p >{&#34;name&#34;: &#34;aaragh&#34;}</a> <script> var data = JSON.parse({&#34;name&#34;: &#34;aaragh&#34;}); console.log(data.name); </script> </body></html> 

I don't think this is relevant, but I get the same error on both ubuntu chrome and win IE.

Any ideas? I think I am missing something obvious, but I hit my head about it for several days and I still have not been able to find a solution ...

thanks

+6
source share
1 answer

Look at the template filter |safe :

 <html><body> <p >{{ result|safe }}</a> <script> var data = JSON.parse({{ result|safe }}); console.log(data.name); </script> </body></html> 

Checkbox docs Jinja docs mentions

+7
source

Source: https://habr.com/ru/post/978301/


All Articles