diff --git a/django/template/defaultfilters.py b/django/template/defaultfilters.py index 673d044e5b..7db6715b49 100644 --- a/django/template/defaultfilters.py +++ b/django/template/defaultfilters.py @@ -681,7 +681,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): @@ -691,7 +694,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): diff --git a/tests/regressiontests/templates/filters.py b/tests/regressiontests/templates/filters.py index 91afdc74ba..e8b1dbe135 100644 --- a/tests/regressiontests/templates/filters.py +++ b/tests/regressiontests/templates/filters.py @@ -320,4 +320,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'}, ''), }