【问题标题】:return html code from python script to html via flask (theory)) [duplicate]通过烧瓶(理论)从python脚本返回html代码到html)[重复]
【发布时间】:2016-12-20 13:34:08
【问题描述】:

我只是在玩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 不将此变量视为代码?如果我直接将&lt;button (code ... for one simple button) .... &gt;&lt;/button&gt; 复制到 index.html 并调用 reneder_template(index.html),一切都很好,但是如果我从 test.py html 中调用此字符串,则这是正文标签中的纯文本...解决这个问题的方法?会很感激。基本上,我希望 html 将此字符串作为代码而不是文本来读取。

【问题讨论】:

标签: python html flask


【解决方案1】:

试试

<body>
{{something | safe}}
</body>

【讨论】:

  • 大声笑,高五,它有效!
猜你喜欢
  • 1970-01-01
  • 2017-04-26
  • 2016-12-12
  • 2020-07-30
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-03
相关资源
最近更新 更多