Issue
I have a simple code:
products_list: list = [{"product_name": "product #1"}, {"product_name": "product #2"}]
return render_template('index.html', data=json.dumps(products_list))
My index template:
<body>
<script>
console.log({{ data }});
</script>
</body>
But on the page i get an invalid json with ", #1" characters.
console.log([{"product_name": "product #1"}, {"product_name": "product #2"}]);
Why it happens?
Solution
The problem here is that flask is escaping the quotes in your JSON by replacing them with "
. You can disable this by adding the |safe
filter to your template.
So your HTML will be:
<body>
<script>
console.log({{ data|safe }});
</script>
</body>
Answered By - TheNightHawk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.