Opened 9 years ago

Closed 6 years ago

#2127 closed defect (fixed)

[patch] date, time, and timesince filters don't fail silently

Reported by: Gary Wilson <gary.wilson@…> Owned by: adrian
Component: Template system Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

if you have something like

{{ event.due_date|date:"F j, Y" }}

in your template, and the date/time field is None, then the template will raise AttributeError:

Traceback (most recent call last):
 File "/usr/lib/python2.4/site-packages/django/template/__init__.py" in render_node
   698. result = node.render(context)
 File "/usr/lib/python2.4/site-packages/django/template/defaulttags.py" in render
   113. nodelist.append(node.render(context))
 File "/usr/lib/python2.4/site-packages/django/template/__init__.py" in render
   744. output = self.filter_expression.resolve(context)
 File "/usr/lib/python2.4/site-packages/django/template/__init__.py" in resolve
   556. obj = func(obj, *arg_vals)
 File "/usr/lib/python2.4/site-packages/django/template/defaultfilters.py" in date
   356. return format(value, arg)
 File "/usr/lib/python2.4/site-packages/django/utils/dateformat.py" in format
   252. return df.format(format_string)
 File "/usr/lib/python2.4/site-packages/django/utils/dateformat.py" in format
   27. pieces.append(str(getattr(self, piece)()))
 File "/usr/lib/python2.4/site-packages/django/utils/dateformat.py" in F
   123. return MONTHS[self.data.month]
 
   AttributeError at /home/
   'NoneType' object has no attribute 'month'

Attachments (1)

filter.diff (1.0 KB) - added by Gary Wilson <gary.wilson@…> 9 years ago.

Download all attachments as: .zip

Change History (6)

Changed 9 years ago by Gary Wilson <gary.wilson@…>

comment:1 Changed 9 years ago by Gary Wilson <gary.wilson@…>

  • Summary changed from date, time, and timesince filters don't fail silently to [patch] date, time, and timesince filters don't fail silently

comment:2 Changed 9 years ago by Gary Wilson <gary.wilson@…>

Just would like to note that a decorator could be used instead if you do one of:

  • Make the function that the decorator returns take the same number of arguments as the filter functions. In this case the parameters (value, arg=None). The downside to this solution would be that the decorator would only work on filter functions that take two parameters.
  • Preserve the functions' signatures. See the comments in #1840 and inability to use decorators on template filters

A decorator that returns an empty string when value is None might be useful to other filters also.

comment:3 Changed 9 years ago by mtredinnick

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

(In [3117]) Fixed #2127 -- Made datetime filters fail silently when passed empty strings or
None. Thanks, Gary Wilson.

comment:5 Changed 6 years ago by kmtracey

  • Resolution set to fixed
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.
Back to Top