#21061 closed Bug (invalid)
is_safe filter flag
Reported by: | Owned by: | polmuz | |
---|---|---|---|
Component: | Template system | Version: | 1.5 |
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
From django/template/base.py:resolve
[code]
if getattr(func, 'is_safe', False) and isinstance(obj, SafeData):
obj = mark_safe(new_obj)
The isinstance(obj, SafeData) seems to prevent is_safe flag from having any affect on the output.
Thinking of the use cases we have some function/tag that we want to either have mark_safe called on, or not called on.
imo, this should just be
[code]
if getattr(func, 'is_safe', False):
obj = mark_safe(new_obj)
This allows is_safe to have an effect on the outcome... which the current code does not.
Change History (3)
comment:1 by , 11 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 11 years ago
Resolution: | → invalid |
---|---|
Status: | assigned → closed |
comment:3 by , 11 years ago
Hi, I did misunderstand the intent of the flag. But while I have your attention on the matter-- the code could still be reduced to
if getattr(func, 'is_safe', False):
as the first thing mark_safe does is to see if it is already an instance of SafeData... but perhaps the intent was to prevent the unnesc call. Thanks
Hi Justin,
I've been going through the code and the docs and that conditional seems to be fine.
The idea is that you may have filter that modifies the input in a safe way but it doesn't return a safe object (e.g. strings are not safe)
So, if the previous object was safe and the filter is safe then it can
be marked as safe again.
resolve()
is doing roughly the following:Here are the docs that describe this behavior https://docs.djangoproject.com/en/1.5/howto/custom-template-tags/#filters-auto-escaping
If you have a filter where you know the output will be safe then you can use
django.utils.safestring.mark_safe
directly on the output.
I may have not understood the description, so if this is not what you meant, please reopen the ticket!