Commits

Anonymous committed f40658e

[1.1.X] Fixed #11687: the `add` filter is now less failsome when faced with things that can't be coerced to integers.

Backport of [12497] from trunk, although the fix here is slightly different to
avoid adding new behavior to a bugfix branch.

  • Participants
  • Parent commits 893d4bb
  • Branches releases/1.1.X

Comments (0)

Files changed (2)

django/template/defaultfilters.py

 
 def add(value, arg):
     """Adds the arg to the value."""
-    return int(value) + int(arg)
+    try:
+        return int(value) + int(arg)
+    except (ValueError, TypeError):
+        return value
 add.is_safe = False
 
 def get_digit(value, arg):

tests/regressiontests/templates/filters.py

         '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'}, ''),
+
+         # Ticket #11687: make sure that add works on int-able things.
+         'add01': (r'{{ i|add:"5" }}', {'i': 2000}, '2005'),
+         'add02': (r'{{ i|add:"napis" }}', {'i': 2000}, '2000'),
+         'add03': (r'{{ i|add:16 }}', {'i': 'not_an_int'}, 'not_an_int'),
+         'add04': (r'{{ i|add:"16" }}', {'i': 'not_an_int'}, 'not_an_int'),
     }