Opened 3 years ago

Closed 3 years ago

#18385 closed Bug (invalid)

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

Reported by: nedbatchelder Owned by: mawimawi
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 3 years ago by nedbatchelder

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

Last edited 3 years ago by nedbatchelder (previous) (diff)

comment:2 Changed 3 years ago by mawimawi

  • Owner changed from nobody to mawimawi

comment:3 Changed 3 years ago by mawimawi

  • Resolution set to invalid
  • Status changed from new to closed

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