Opened 13 years ago

Closed 10 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: master
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


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/" in render_node
   698. result = node.render(context)
 File "/usr/lib/python2.4/site-packages/django/template/" in render
   113. nodelist.append(node.render(context))
 File "/usr/lib/python2.4/site-packages/django/template/" in render
   744. output = self.filter_expression.resolve(context)
 File "/usr/lib/python2.4/site-packages/django/template/" in resolve
   556. obj = func(obj, *arg_vals)
 File "/usr/lib/python2.4/site-packages/django/template/" in date
   356. return format(value, arg)
 File "/usr/lib/python2.4/site-packages/django/utils/" in format
   252. return df.format(format_string)
 File "/usr/lib/python2.4/site-packages/django/utils/" in format
   27. pieces.append(str(getattr(self, piece)()))
 File "/usr/lib/python2.4/site-packages/django/utils/" in F
   123. return MONTHS[]
   AttributeError at /home/
   'NoneType' object has no attribute 'month'

Attachments (1)

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

Download all attachments as: .zip

Change History (6)

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

Attachment: filter.diff added

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

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

comment:2 Changed 13 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 13 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

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

comment:5 Changed 10 years ago by Karen Tracey

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