python - 通过flask将html代码从python脚本返回到html(理论))

标签 python html flask

我只是在玩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/

相关文章:

python - 修复密码哈希在导入到 MySQL 表时被截断的问题

amazon-web-services - 无法通过来自 Python Elastic Beanstalk 的 Flask SQLAlchemy URI 连接到 AWS MySQL RDS 实例

heroku - 从私有(private) AWS S3 存储桶在 Flask 中提供静态文件

python - Pandas 数据帧到对象实例数组的批量数据库插入效率

python - 获取二维数组中局部最大值的坐标高于某个值

python - Panda 的箱线图但未显示箱线

iphone - 如何应用亮度控制和更改文本颜色?

javascript - Markdown 美化

javascript - 导航栏悬停,显示子导航 html

python - 每 3 个数字将列表中的数字相加