Opened 6 years ago

Closed 6 years ago

#18385 closed Bug (invalid)

firstof doesn't deal well with empty "safe" values.

Reported by: Ned Batchelder Owned by: Martin Winkler
Component: Template system Version: 1.4
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


>>> from django.template import Context, Template
>>> t = Template("Value: {{ value|safe }}, Firstof: {% firstof value|safe 'Hello' %}")
>>> t.render(Context({}))
u'Value: , Firstof: None'

Here, {{ value|safe }} produced an empty string, but value|safe inside of firstof produced None.

Change History (3)

comment:1 Changed 6 years ago by Ned Batchelder

I suspect this is because SafeString (and SafeUnicode), being user-defined objects, are always true, regardless of the data they wrap.

Last edited 6 years ago by Ned Batchelder (previous) (diff)

comment:2 Changed 6 years ago by Martin Winkler

Owner: changed from nobody to Martin Winkler

comment:3 Changed 6 years ago by Martin Winkler

Resolution: invalid
Status: newclosed

according to the documentation of the firstof tag:

Outputs the first variable passed that is not False. Does NOT auto-escape variable values.

So the safe filter is unnecessary, because the results have to be safe anyway.

Note: See TracTickets for help on using tickets.
Back to Top