Django模板中的转义问题 [英] escape problem in django templates

查看:37
本文介绍了Django模板中的转义问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个字符串:

s = '<p>Hello!</p>'

当我将此变量传递给模板时,我希望将其呈现为原始html.查看文档,我发现我可以使用安全过滤器:

When I pass this variable to a template, I want it to be rendered as raw html. Looking at the docs I see that I can either use the safe filter:

{{s|safe}}

或禁用自动转义:

{%autoescape off}
{{s}}
{%endautoescape%}

或在python代码中声明它是安全的:

or inside the python code declare it safe:

from django.utils.safestring import mark_safe
s = mark_safe(s)

这些选项都不对我有用.无论我做什么,字符串都显示为:

None of these options are working for me. Whatever I do, the string is displayed as:

<p>Hello!</p>

我一定想念什么,只是想不通.某处是否存在一些安全设置,不允许转义?

I must be missing something, just couldn't figure out what. Is there some security setting somewhere that disallows escaping?

奇怪,重新启动计算机后问题似乎消失了.

Bizarre, the problem seems to be gone after I have restarted the computer.

推荐答案

您几乎涵盖了它,这些确实是禁用自动转义的所有方法.

You pretty much covered it, those are indeed all the ways to disable autoescaping.

您确定要说的值实际上是 s ='< p> Hello!</p>'吗?

Are you sure the value you are talking about is actually s = '<p>Hello!</p>'?

我的直觉是您在该字符串中的某处还有其他转义...

My hunch is that you have additional escaping somewhere in that string...

这篇关于Django模板中的转义问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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