Fixed #29288 -- Made {% widthratio %} assign to as var if an exception occurs.
This commit is contained in:
parent
9fd9f8bbb2
commit
6148dda72f
|
@ -483,9 +483,9 @@ class WidthRatioNode(Node):
|
||||||
ratio = (value / max_value) * max_width
|
ratio = (value / max_value) * max_width
|
||||||
result = str(round(ratio))
|
result = str(round(ratio))
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
return '0'
|
result = '0'
|
||||||
except (ValueError, TypeError, OverflowError):
|
except (ValueError, TypeError, OverflowError):
|
||||||
return ''
|
result = ''
|
||||||
|
|
||||||
if self.asvar:
|
if self.asvar:
|
||||||
context[self.asvar] = result
|
context[self.asvar] = result
|
||||||
|
|
|
@ -142,3 +142,13 @@ class WidthRatioTagTests(SimpleTestCase):
|
||||||
def test_widthratio21(self):
|
def test_widthratio21(self):
|
||||||
output = self.engine.render_to_string('widthratio21', {'a': float('inf'), 'b': 2})
|
output = self.engine.render_to_string('widthratio21', {'a': float('inf'), 'b': 2})
|
||||||
self.assertEqual(output, '')
|
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, '--')
|
||||||
|
|
Loading…
Reference in New Issue