将模板变量呈现为HTML [英] Rendering a template variable as HTML

查看:98
本文介绍了将模板变量呈现为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用消息界面将消息传递给用户,如下所示:

I use the 'messages' interface to pass messages to user like this:

request.user.message_set.create(message=message)

我想在我的 {{message }} 变量,并在不转义模板中的标记的情况下呈现它。

I would like to include html in my {{ message }} variable and render it without escaping the markup in the template.

推荐答案

不要HTML转义,看看安全过滤器和 autoescape 标签

If you dont't want HTML escaped, look at the safe filter and the autoescape tag

FILTER: {{myhtml | safe}}

http://docs.djangoproject.com/en/dev/ref/templates/builtins/#safe

标签: {%autoescape off%} {{myhtml}} {%endautoescape%}
http://docs.djangoproject.com/en/dev/ref/templates/builtins/#autoescape

这篇关于将模板变量呈现为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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