#8966 closed (fixed)
{% if "x"|length_is:n %} always evaluates True
Reported by: | Owned by: | Carl Meyer | |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
{% if "x"|capfirst|length_is:n %} is always true. Example:
In [1]: from django.template import Template, Context In [2]: Template('{% if "x"|capfirst|length_is:0 %}This is wrong{% endif %}').render(Context({})) Out[2]: u'This is wrong'
Using latest Django SVN.
Attachments (3)
Change History (9)
comment:1 by , 16 years ago
Has patch: | set |
---|---|
Summary: | {% if "x"|capfirst|length_is:n %} always evaluates True → {% if "x"|length_is:n %} always evaluates True |
Version: | 1.0 → SVN |
by , 16 years ago
Attachment: | 8966_naive_fix.diff added |
---|
simple fix (remove is_safe from length_is filter)
comment:2 by , 16 years ago
Component: | Uncategorized → Template system |
---|
comment:3 by , 16 years ago
Triage Stage: | Unreviewed → Ready for checkin |
---|
Removal of is_safe is the right thing to do. It's a gotcha though -- a note explaining that is_safe
shouldn't be used if you're relying on a non-string return object in docs/howto/custom-template-tags.txt
would be useful.
comment:4 by , 16 years ago
Owner: | changed from | to
---|
comment:5 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:6 by , 16 years ago
Note:
See TracTickets
for help on using tickets.
capfirst is irrelevant. This also fails:
Changed summary to reflect this. Also changed version to SVN, as this problem is still present in trunk.
The problem here is that the is_safe attribute is set on the length_is tag, which causes mark_safe to be called on the return value, and mark_safe(False) returns the string 'False'. Not sure where in that chain is the proper place for the fix. It's possible that mark_safe needs to be smart about not always coercing its argument to a string, or FilterExpression.resolve needs to check if the return value is a string before calling mark_safe on it. But the naive fix is just to remove the is_safe attribute from length_is, since auto-escaping will never harm its return value.