我只是在玩Python脚本(test.py),有一个函数test1()
.
def test1():
return '<button (code ... for one simple button) .... ></button>'
在我的init.py中:
from flask import Flask
from flask import render_template, url_for
import test
abc = test.test1()
app = Flask(__name__)
@app.route("/")
def index():
try:
return render_template('index.html', something=abc)
except Exception as e:
return str(e)
if __name__ == '__main__':
app.run()
在我的index.html 中我有:
<body>
{{something}}
</body>
为什么 html 不将此变量视为代码?如果我复制<button (code ... for one simple button) .... ></button>
直接到index.html,我只是调用reneder_template(index.html),一切都很好,但是如果我从test.py html调用这个字符串,那么这是正文标签内的纯文本...有没有办法解决这个问题?会很感激的。基本上,我希望 html 将此字符串作为代码而不是文本来读取。
最佳答案
尝试
<body>
{{something | safe}}
</body>
关于python - 通过flask将html代码从python脚本返回到html(理论)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41243622/