diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
index c08ecc4..b31de38 100644
a
|
b
|
class WidthRatioNode(Node):
|
395 | 395 | try: |
396 | 396 | value = self.val_expr.resolve(context) |
397 | 397 | maxvalue = self.max_expr.resolve(context) |
| 398 | max_width = int(self.max_width.resolve(context)) |
398 | 399 | except VariableDoesNotExist: |
399 | 400 | return '' |
| 401 | except ValueError: |
| 402 | raise TemplateSyntaxError("widthratio final argument must be an integer") |
400 | 403 | try: |
401 | 404 | value = float(value) |
402 | 405 | maxvalue = float(maxvalue) |
403 | | ratio = (value / maxvalue) * int(self.max_width) |
| 406 | ratio = (value / maxvalue) * max_width |
404 | 407 | except (ValueError, ZeroDivisionError): |
405 | 408 | return '' |
406 | 409 | return str(int(round(ratio))) |
… |
… |
def widthratio(parser, token):
|
1136 | 1139 | if len(bits) != 4: |
1137 | 1140 | raise TemplateSyntaxError("widthratio takes three arguments") |
1138 | 1141 | tag, this_value_expr, max_value_expr, max_width = bits |
1139 | | try: |
1140 | | max_width = int(max_width) |
1141 | | except ValueError: |
1142 | | raise TemplateSyntaxError("widthratio final argument must be an integer") |
| 1142 | |
1143 | 1143 | return WidthRatioNode(parser.compile_filter(this_value_expr), |
1144 | | parser.compile_filter(max_value_expr), max_width) |
| 1144 | parser.compile_filter(max_value_expr), |
| 1145 | parser.compile_filter(max_width)) |
1145 | 1146 | widthratio = register.tag(widthratio) |
1146 | 1147 | |
1147 | 1148 | #@register.tag |
diff --git a/tests/regressiontests/templates/tests.py b/tests/regressiontests/templates/tests.py
index 042aba2..9e4bba9 100644
a
|
b
|
class Templates(unittest.TestCase):
|
908 | 908 | # Raise exception if we don't have 3 args, last one an integer |
909 | 909 | 'widthratio08': ('{% widthratio %}', {}, template.TemplateSyntaxError), |
910 | 910 | 'widthratio09': ('{% widthratio a b %}', {'a':50,'b':100}, template.TemplateSyntaxError), |
911 | | 'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, template.TemplateSyntaxError), |
| 911 | 'widthratio10': ('{% widthratio a b 100.0 %}', {'a':50,'b':100}, '50'), |
| 912 | |
| 913 | #Ticket 10043: Widthratio should allow maxlength to be a variable |
| 914 | 'widthratio11': ('{% widthratio a b c %}', {'a':50,'b':100, 'c': 100}, '50'), |
912 | 915 | |
913 | 916 | ### WITH TAG ######################################################## |
914 | 917 | 'with01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key':50}}, '50'), |