html - 将 html 作为模板变量传递 django
问题描述
我像这样将一些 html 传递给我的模板passDict["problemText"] = <p> Some html</p>
return render(response,'main/base.html',passDict)
。然后显示{{problemText}}
在我的 html 文件中。我得到<p> Some html</p>
的是我的文本,而不是Some html
我想要的段落。
解决方案
它需要被标记为安全。
使用safe
过滤器。
{{ problemText|safe }}
或者使用mark_safe()
方法。
from django.utils.safestring import mark_safe
problemText = mark_safe("<p>Some html</p>")
阅读有关安全过滤器的文档和mark_safe()
.
推荐阅读
- asp.net-core - 如何在 .Net Core Identity 中强制执行 2FA?
- android - 如何在 xml 和 kotlin 组合中创建类似布局的计算器
- python-3.x - Python:您能否根据用户输入的 int 的索引显示单个 int
- android - 如何在 Android Studio 中升级 http 依赖
- python - 在 Tensorflow 中使用 deltaE_ciede2000 查找颜色距离
- python - 如何在python的框架(tkinter)内的标签中显示我的覆盆子凸轮?
- google-sheets - 如何计算 Google 表格单元格中文本的出现次数?
- sql - Oracle分层查询数据
- html - Why does the CSS not work with my button element?
- javascript - 如何在 Cloudflare Workers 中设置 CORS?