escape problem in django templates

青春壹個敷衍的年華 提交于 2021-02-06 10:48:28

问题


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.


回答1:


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...




回答2:


I think you should write as follows

{{s|escape|safe}}

it is ok for me




回答3:


Look at the HTML source code, is it escaped!??? I don't think so. It should be printing letter by letter, just like this:

<
p
>
H
E
L
L
O
<
/
>


来源:https://stackoverflow.com/questions/871163/escape-problem-in-django-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!