escape problem in django templates
Solution 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...
Solution 2
I think you should write as follows
{{s|escape|safe}}
it is ok for me
shanyu
Mechanical Engineering + MBA + PhD in finance. Python & Javascript.
Updated on July 20, 2022Comments
-
shanyu almost 2 years
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.
-
shanyu almost 15 yearsI have even tried this hello string. Actually I have the same issue with all the strings I pass to the templates or send as an e-mail.
-
Mr. BeatMasta over 8 yearsThanks, you saved my life and hours of digging through google
-
datalifenyc about 3 yearsInterestingly, the documentation suggests that you put
escape
aftersafe
, the variable will print as is. However, that is not the behavior I am seeing.{{ var|safe|escape }}
renders the html.{{ var|escape|safe }}
renders the variable text as-is.