Opened 18 years ago

Closed 16 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 Holovaty
Component: Template system Version: dev
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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@…> 18 years ago.

Download all attachments as: .zip

Change History (6)

by Gary Wilson <gary.wilson@…>, 18 years ago

Attachment: filter.diff added

comment:1 by Gary Wilson <gary.wilson@…>, 18 years ago

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

comment:2 by Gary Wilson <gary.wilson@…>, 18 years ago

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 by Malcolm Tredinnick, 18 years ago

Resolution: fixed
Status: newclosed

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

comment:5 by Karen Tracey, 16 years ago

Resolution: fixed
Status: reopenedclosed
Note: See TracTickets for help on using tickets.
Back to Top