Fixed #29288 -- Made {% widthratio %} assign to as var if an exception occurs.

This commit is contained in:
Jirka Vejrazka 2018-04-04 20:03:16 +02:00 committed by Tim Graham
parent 9fd9f8bbb2
commit 6148dda72f
2 changed files with 12 additions and 2 deletions

View File

@ -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

View File

@ -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, '--')