#29791 closed Bug (fixed)
Engine.render_to_string() should honor the autoescape attribute
Reported by: | Claude Paroz | Owned by: | Nathan Gaberel |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Claude Paroz | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In Engine.render_to_string
, a Context
is created without specifying the engine autoescape
attribute. So if you create en engine with autoescape=False
and then call its render_to_string()
method, the result will always be autoescaped. It was probably overlooked in [19a5f6da329d58653bcda85].
Attachments (3)
Change History (10)
comment:1 by , 6 years ago
Summary: | Engine.render_to_string should honor the autoescape attribute → Engine.render_to_string() should honor the autoescape attribute |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 6 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 6 years ago
I'd like to give this a try. Is it OK to reassign to myself since there's been no update in 6 months?
comment:4 by , 6 years ago
Cc: | added |
---|
I think it's safe to re-assign yourself the ticket to you at this point. Are you still planing on working on this in the near future Claude?
comment:5 by , 6 years ago
Has patch: | set |
---|---|
Owner: | changed from | to
I went ahead and wrote a patch for it, hopefully Claude won't mind. :)
PR
by , 3 years ago
Attachment: | autoescape and safe ignored 2021-08-31 16-15-39.png added |
---|
autoescape off and safe filter both ignored, html entities applied
by , 3 years ago
Attachment: | still escaped 2021-08-31 20-11-56.png added |
---|
get_absolute_url shown as removed in chrome dev tools. full name with domain does show.
by , 3 years ago
Attachment: | library.html still escaped after restart and turning off in PyCharm actions 2021-08-31 20-06-12.png added |
---|
link destination in bottom left of screen shows html entities escaping my code
comment:7 by , 3 years ago
I came here because I cannot get autoescape to turn off. First I put it in my template, surrounding the links I wanted to work, but it did not. I looked around and saw that I could control it the same way template inheritance works, so I went to my base html template and put the autoescape tags there. It still had no effect. Then I rebooted, hoping that would make a difference. It did not.
I do not understand how this patch works. Given that at line 20 of engine.py, the init has
autoescape = True,
what are
self.autoescape=autoescape [line 44]
and
autoescape=self.autoescape [line 163]
actually doing? It just looks like a circular assignment to me.
Further, I don’t see how this is connected to template tags. I see no reference to tags in any of this code. Perhaps it is somewhere else, and happens magically, but regardless, I don’t see it.
Finally, I do not understand the test you ran.
self.assertEqual( engine.render_to_string('test_context.html', {'obj': '<script>'}), 'obj:<script>\n', )
It looks to me like all you are doing here is removing the curly braces from a dict, and adding a newline. What has that got to do with autoescaping html? The angle brackets in your test object are still there.
In my templates, all that html is removed in favor of html entities - as you can see from the screen shots I attached.. That means the links don't work, don’t display, and give me repeated NoReverseMatch errors even when the code Django tried was correct.
I would ask that you re-open this ticket and make the autoescape tag work as advertised.
I'd like to reserve this for an event I'll attend on Oct 4th.