在 django 模板中将字符串转换为 html 代码

2022-01-18 00:00:00 django string django-templates tags 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

相关文章