python flask body_html 字符串替换_从Python Flask以&#34形式传递给HTML页面的字符串...

i'm developing Flask App.

I want to transfer simple json from the app.py to the html page.

This is the relevant code at app.py:

jsonArr = [{"type": "circle", "label": "New York"},

{"type": "circle", "label": "New York"}]

return render_template('demo.html', foo=42, imgs=jsonArr)

This is how I receive it it javascript script inside the html:

console.log("HI")

var foo = {{ foo }}

console.log(foo)

var images = {{ imgs }}

console.log(images)

foo is received correctly (I see the printing on the console when I remove the lines for receiving imgs)

But imgs makes error: Uncaught SyntaxError: Unexpected token &

This is what I see in the chrome browser sources:

var images = [['circle', 'New York'], ['triangle', 'Amsterdam']]

This is the html declerations:

I've tried adding/removing but it didn't work.

What am I missing? Thanks

解决方案

In order to avoid cross-site-scripting attacks, flask automatically escapes HTML sequences. If you want to avoid this, you can directly tell Flask you know what you're doing:

from flask import Markup

value = Markup('The HTML String')

However!! This is risky from a security perspective. If you have any user data that can end up in the JSON, you need to consider another approach.

You would need to sanitize the JSON so a user doesn't come along with a string of

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值