diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 14391f08e1d..cb2ecd26d8f 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -448,7 +448,7 @@ class WidthRatioNode(Node): max_width = int(self.max_width.resolve(context)) except VariableDoesNotExist: return '' - except ValueError: + except (ValueError, TypeError): raise TemplateSyntaxError("widthratio final argument must be an number") try: value = float(value) @@ -456,7 +456,7 @@ class WidthRatioNode(Node): ratio = (value / max_value) * max_width except ZeroDivisionError: return '0' - except ValueError: + except (ValueError, TypeError): return '' return str(int(round(ratio))) diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index edbb21b6bd8..a21b7ff323d 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -1466,6 +1466,14 @@ class Templates(unittest.TestCase): # #10043: widthratio should allow max_width to be a variable 'widthratio11': ('{% widthratio a b c %}', {'a':50,'b':100, 'c': 100}, '50'), + # #18739: widthratio should handle None args consistently with non-numerics + 'widthratio12a': ('{% widthratio a b c %}', {'a':'a','b':100,'c':100}, ''), + 'widthratio12b': ('{% widthratio a b c %}', {'a':None,'b':100,'c':100}, ''), + 'widthratio13a': ('{% widthratio a b c %}', {'a':0,'b':'b','c':100}, ''), + 'widthratio13b': ('{% widthratio a b c %}', {'a':0,'b':None,'c':100}, ''), + 'widthratio14a': ('{% widthratio a b c %}', {'a':0,'b':100,'c':'c'}, template.TemplateSyntaxError), + 'widthratio14b': ('{% widthratio a b c %}', {'a':0,'b':100,'c':None}, template.TemplateSyntaxError), + ### WITH TAG ######################################################## 'with01': ('{% with key=dict.key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'), 'legacywith01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'),