【问题标题】:Passing html as a template variable django将 html 作为模板变量传递 django
【发布时间】:2020-05-05 16:36:15
【问题描述】:

我正在将一些 html 传递给我的模板,例如 passDict["problemText"] = <p> Some html</p> return render(response,'main/base.html',passDict)。然后在我的html文件中显示{{problemText}}。我得到&lt;p&gt; Some html&lt;/p&gt;作为我的文本,而不是Some html在我想要的段落中。

【问题讨论】:

    标签: html django templates


    【解决方案1】:

    它需要被标记为安全。

    使用safe 过滤器。

    {{ problemText|safe }}
    

    或者使用mark_safe()方法。

    from django.utils.safestring import mark_safe
    
    problemText = mark_safe("<p>Some html</p>")
    

    阅读safe filtermark_safe() 上的文档。

    【讨论】:

      猜你喜欢
      • 2012-10-11
      • 2019-04-03
      • 2013-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-09
      相关资源
      最近更新 更多