Skip to content

Commit

Permalink
[1.0.X] Fixed #9520: make the date filter fail silently for non-date …
Browse files Browse the repository at this point in the history
…values. Thanks, Andrew Badr and Eric Holscher. Backport of r10365 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10366 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jacobian committed Apr 2, 2009
1 parent 38aeee4 commit 4257fef
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 8 additions & 2 deletions django/template/defaultfilters.py
Expand Up @@ -673,7 +673,10 @@ def date(value, arg=None):
return u''
if arg is None:
arg = settings.DATE_FORMAT
return format(value, arg)
try:
return format(value, arg)
except AttributeError:
return ''
date.is_safe = False

def time(value, arg=None):
Expand All @@ -683,7 +686,10 @@ def time(value, arg=None):
return u''
if arg is None:
arg = settings.TIME_FORMAT
return time_format(value, arg)
try:
return time_format(value, arg)
except AttributeError:
return ''
time.is_safe = False

def timesince(value, arg=None):
Expand Down
6 changes: 6 additions & 0 deletions tests/regressiontests/templates/filters.py
Expand Up @@ -317,4 +317,10 @@ def get_filter_tests():
'join02': (r'{% autoescape off %}{{ a|join:", " }}{% endautoescape %}', {'a': ['alpha', 'beta & me']}, 'alpha, beta & me'),
'join03': (r'{{ a|join:" & " }}', {'a': ['alpha', 'beta & me']}, 'alpha & beta & me'),
'join04': (r'{% autoescape off %}{{ a|join:" & " }}{% endautoescape %}', {'a': ['alpha', 'beta & me']}, 'alpha & beta & me'),


'date01': (r'{{ d|date:"m" }}', {'d': datetime(2008, 1, 1)}, '01'),
'date02': (r'{{ d|date }}', {'d': datetime(2008, 1, 1)}, 'Jan. 1, 2008'),
#Ticket 9520: Make sure |date doesn't blow up on non-dates
'date03': (r'{{ d|date:"m" }}', {'d': 'fail_string'}, ''),
}

0 comments on commit 4257fef

Please sign in to comment.