首页 > 解决方案 > 将 html 作为模板变量传递 django

问题描述

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

标签: htmldjangotemplates

解决方案


它需要被标记为安全。

使用safe过滤器。

{{ problemText|safe }}

或者使用mark_safe()方法。

from django.utils.safestring import mark_safe

problemText = mark_safe("<p>Some html</p>")

阅读有关安全过滤器的文档和mark_safe().


推荐阅读