【问题标题】: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}}。我得到<p> Some html</p>作为我的文本,而不是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 filter 和mark_safe() 上的文档。