在 django 模板中将字符串转换为 html 代码 [英] convert string to html code in django template

查看:68
本文介绍了在 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 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆