From a4e516b593a5d4bd81a160b3b1a8cb4b23821b7d Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 17 Dec 2011 10:27:14 +0000 Subject: [PATCH] Fixed #11166 -- {% widthratio %} should return 0 when the maximum is 0, no matter the value. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17224 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/defaulttags.py | 10 ++++++---- tests/regressiontests/templates/tests.py | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index b7c36f5710..7e2ee6758f 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -435,7 +435,7 @@ class WidthRatioNode(Node): def render(self, context): try: value = self.val_expr.resolve(context) - maxvalue = self.max_expr.resolve(context) + max_value = self.max_expr.resolve(context) max_width = int(self.max_width.resolve(context)) except VariableDoesNotExist: return '' @@ -443,9 +443,11 @@ class WidthRatioNode(Node): raise TemplateSyntaxError("widthratio final argument must be an number") try: value = float(value) - maxvalue = float(maxvalue) - ratio = (value / maxvalue) * max_width - except (ValueError, ZeroDivisionError): + max_value = float(max_value) + ratio = (value / max_value) * max_width + except ZeroDivisionError: + return '0' + except ValueError: return '' return str(int(round(ratio))) diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py index 4e0c1f19e4..1a1b360847 100644 --- a/tests/regressiontests/templates/tests.py +++ b/tests/regressiontests/templates/tests.py @@ -1429,7 +1429,7 @@ class Templates(unittest.TestCase): ### WIDTHRATIO TAG ######################################################## 'widthratio01': ('{% widthratio a b 0 %}', {'a':50,'b':100}, '0'), - 'widthratio02': ('{% widthratio a b 100 %}', {'a':0,'b':0}, ''), + 'widthratio02': ('{% widthratio a b 100 %}', {'a':0,'b':0}, '0'), 'widthratio03': ('{% widthratio a b 100 %}', {'a':0,'b':100}, '0'), 'widthratio04': ('{% widthratio a b 100 %}', {'a':50,'b':100}, '50'), 'widthratio05': ('{% widthratio a b 100 %}', {'a':100,'b':100}, '100'),