将字符串转换为 django 模板中的 html 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7786493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
convert string to html code in django template
提问by user712378
Possible Duplicate:
django: rendering a template variable as html
可能的重复:
django:将模板变量呈现为 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.
我正在开发一个 django 站点,我有一个字符串变量,其中包含 html 标签。我需要将该字符串作为模板上的 html 代码读取。
For instance if I have a string variable
例如,如果我有一个字符串变量
description = "<ul><li>abc</li><li>def</li><li>ghi</li></ul>
"
描述 = " <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
- def
- ghi
- 美国广播公司
- 定义
- 吉
Currently it shows the string as it is.
目前它按原样显示字符串。
I would really appreciate any help with this.
我真的很感激这方面的任何帮助。
回答by Timmy O'Mahony
{{ description | safe }}
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#safe
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#safe
more
更多的
https://docs.djangoproject.com/en/dev/topics/templates/#automatic-html-escaping
https://docs.djangoproject.com/en/dev/topics/templates/#automatic-html-escaping