假设我有这个字符串:
s = '<p>Hello!</p>'
当我将此变量传递给模板时,我希望它呈现为原始 html。 查看文档,我发现我可以使用安全过滤器:
{{s|safe}}
或禁用自动转义:
{%autoescape off}
{{s}}
{%endautoescape%}
或者在Python代码中声明它安全:
from django.utils.safestring import mark_safe
s = mark_safe(s)
这些选项都不适合我。 无论我做什么,该字符串都会显示为:
<p>Hello!</p>
我一定错过了一些东西,只是不知道是什么。 是否有某些安全设置不允许逃逸?
编辑:奇怪的是,在我重新启动计算机后,问题似乎消失了。
Let's say that I have this string:
s = '<p>Hello!</p>'
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}}
or disable autoescape:
{%autoescape off}
{{s}}
{%endautoescape%}
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?
EDIT: Bizarre, the problem seems to be gone after I have restarted the computer.
发布评论
评论(3)
我认为你应该写如下,
这对我来说没问题
I think you should write as follows
it is ok for me
您几乎涵盖了它,这些确实是禁用自动转义的所有方法。
您确定您正在谈论的值实际上是
s = '
Hello!
'
吗?我的预感是你在该字符串的某个地方有额外的转义......
You pretty much covered it, those are indeed all the ways to disable autoescaping.
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...
看看HTML源码,是不是转义了!??? 我不这么认为。 它应该一个字母一个字母地打印,就像这样:
Look at the HTML source code, is it escaped!??? I don't think so. It should be printing letter by letter, just like this: