Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #18739 -- witdthratio behavior on None args

Made behavior given None consistent with how non-numerics were handled.
Thanks to ja.geb@me.com for the report.
  • Loading branch information...
commit 759ae3c2dafe149485a15d9a3bc97527c1ac1163 1 parent 15fffcc
@kmtracey kmtracey authored
View
4 django/template/defaulttags.py
@@ -448,7 +448,7 @@ def render(self, context):
max_width = int(self.max_width.resolve(context))
except VariableDoesNotExist:
return ''
- except ValueError:
+ except (ValueError, TypeError):
raise TemplateSyntaxError("widthratio final argument must be an number")
try:
value = float(value)
@@ -456,7 +456,7 @@ def render(self, context):
ratio = (value / max_value) * max_width
except ZeroDivisionError:
return '0'
- except ValueError:
+ except (ValueError, TypeError):
return ''
return str(int(round(ratio)))
View
8 tests/regressiontests/templates/tests.py
@@ -1466,6 +1466,14 @@ def get_template_tests(self):
# #10043: widthratio should allow max_width to be a variable
'widthratio11': ('{% widthratio a b c %}', {'a':50,'b':100, 'c': 100}, '50'),
+ # #18739: widthratio should handle None args consistently with non-numerics
+ 'widthratio12a': ('{% widthratio a b c %}', {'a':'a','b':100,'c':100}, ''),
+ 'widthratio12b': ('{% widthratio a b c %}', {'a':None,'b':100,'c':100}, ''),
+ 'widthratio13a': ('{% widthratio a b c %}', {'a':0,'b':'b','c':100}, ''),
+ 'widthratio13b': ('{% widthratio a b c %}', {'a':0,'b':None,'c':100}, ''),
+ 'widthratio14a': ('{% widthratio a b c %}', {'a':0,'b':100,'c':'c'}, template.TemplateSyntaxError),
+ 'widthratio14b': ('{% widthratio a b c %}', {'a':0,'b':100,'c':None}, template.TemplateSyntaxError),
+
### WITH TAG ########################################################
'with01': ('{% with key=dict.key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'),
'legacywith01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'),

1 comment on commit 759ae3c

@slav0nic

{'a':0,'b':100,'c':'c'} pep8 ...

Please sign in to comment.
Something went wrong with that request. Please try again.