convert string to html code in django template(在 django 模板中将字符串转换为 html 代码)
问题描述
可能重复:
django:将模板变量渲染为html
我正在开发一个 django 站点,并且我有一个字符串变量,其中包含 html 标记.我需要将该字符串作为模板上的 html 代码读取.
I am developing a django site and I have a string variable which has html tags in it. I need that string to be read as html code on my template.
例如,如果我有一个字符串变量
For instance if I have a string variable
description = "<ul><li>abc</li><li>def</li><li>ghi</li></ul>
"
description = "<ul><li>abc</li><li>def</li><li>ghi</li></ul>
"
当我在模板中调用这个字符串变量时,我希望它显示为
When I call this string variable in my template, I would like it to be displayed as
- abc
- 定义
- ghi
目前它按原样显示字符串.
Currently it shows the string as it is.
非常感谢您对此提供任何帮助.
I would really appreciate any help with this.
推荐答案
{{ description | safe }}
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#safe
更多
https://docs.djangoproject.com/en/dev/topics/模板/#automatic-html-escaping
这篇关于在 django 模板中将字符串转换为 html 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 django 模板中将字符串转换为 html 代码


基础教程推荐
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01