【问题标题】:Inserting HTML code with Flask [duplicate]使用 Flask 插入 HTML 代码 [重复]
【发布时间】:2016-10-12 01:55:18
【问题描述】:

目前我有这个代码

boldtext = "<b>I'm bolded!<b>"

@app.route('/')
def index():
     return render_template("index.html", boldtext=boldtext)

然后在我的 index.html 中,我有一段代码,其中包含 {{ boldtext }}。

我原以为页面加载后我会看到:

我是加粗的!

相反,我看到的是:

<b>I'm bolded!<b>

我是否可以将实际的 HTML 代码传递给 index.html 而不是纯文本的 sn-p?

【问题讨论】:

    标签: python html flask


    【解决方案1】:

    您正在寻找safe filter

    {{ boldtext|safe }}
    

    【讨论】:

      【解决方案2】:

      好的,我实际上很快就找到了答案,我之前尝试过,但由于语法问题,它不起作用。我所要做的就是通过做导入标记。

      from flask import Markup
      

      然后将粗体文本定义为

      boldtext = "<b>I'm bolded!<b>"
      

      相反

      boldtext = Markup("<b>I'm bolded!<b>")
      

      【讨论】:

      • 如何在 Markup sir 中插入变量?
      猜你喜欢
      • 2017-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-18
      相关资源
      最近更新 更多