diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 6e52249584..43c557f179 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -483,9 +483,9 @@ class WidthRatioNode(Node): ratio = (value / max_value) * max_width result = str(round(ratio)) except ZeroDivisionError: - return '0' + result = '0' except (ValueError, TypeError, OverflowError): - return '' + result = '' if self.asvar: context[self.asvar] = result diff --git a/tests/template_tests/syntax_tests/test_width_ratio.py b/tests/template_tests/syntax_tests/test_width_ratio.py index bec7d93a4c..7db90a44e1 100644 --- a/tests/template_tests/syntax_tests/test_width_ratio.py +++ b/tests/template_tests/syntax_tests/test_width_ratio.py @@ -142,3 +142,13 @@ class WidthRatioTagTests(SimpleTestCase): def test_widthratio21(self): output = self.engine.render_to_string('widthratio21', {'a': float('inf'), 'b': 2}) self.assertEqual(output, '') + + @setup({'t': '{% widthratio a b 100 as variable %}-{{ variable }}-'}) + def test_zerodivisionerror_as_var(self): + output = self.engine.render_to_string('t', {'a': 0, 'b': 0}) + self.assertEqual(output, '-0-') + + @setup({'t': '{% widthratio a b c as variable %}-{{ variable }}-'}) + def test_typeerror_as_var(self): + output = self.engine.render_to_string('t', {'a': 'a', 'c': 100, 'b': 100}) + self.assertEqual(output, '--')