summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJirka Vejrazka <Jirka.Vejrazka@gmail.com>2018-04-04 20:03:16 +0200
committerTim Graham <timograham@gmail.com>2018-04-04 14:03:16 -0400
commit6148dda72f47e40754fc2eb75d86274fb68f2d41 (patch)
treead9ed9efe7475fde98481df7ab367b39eec5be2a
parent9fd9f8bbb23407254aa0ba851dbcc8e7f696c3de (diff)
Fixed #29288 -- Made {% widthratio %} assign to as var if an exception occurs.
-rw-r--r--django/template/defaulttags.py4
-rw-r--r--tests/template_tests/syntax_tests/test_width_ratio.py10
2 files changed, 12 insertions, 2 deletions
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, '--')