Opened 3 years ago
Closed 3 years ago
#33603 closed Cleanup/optimization (wontfix)
Backslashes in template tag arguments are escaped
Reported by: | bhch | Owned by: | nobody |
---|---|---|---|
Component: | Template system | Version: | 3.2 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I've written a custom template tag to replace parts of a value:
@register.simple_tag def string_replace(value, old, new): return value.replace(old, new)
And I'm using it to replace newline characters with commas in a user's address:
{% string_replace user.address '\r\n' ',' %}
However, this doesn't work.
The reason is that when Django loads a template using Python's file.read()
function, the backslashes get escaped which means \r\n
becomes \\r\\n
.
So, the string_replace
tag actually receives \\r\\n
instead of \r\n
. This also affects other tags as well.
Shouldn't Django pass the original string argument to the template tag as intended?
Change History (1)
comment:1 by , 3 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Type: | Bug → Cleanup/optimization |
Thanks for the report, however I don't think Django can do much here, and any change would be backward incompatible. Please also take into account that it works when
value
is passed directly, e.g.I'd recommend to create a new filter, e.g.
{% replace_newline value %}
, pass arguments via context variables, or escape/unescape arguments instring_replace()
.