mirror of https://github.com/django/django.git
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.
This commit is contained in:
parent
15fffcc751
commit
759ae3c2da
|
@ -448,7 +448,7 @@ class WidthRatioNode(Node):
|
||||||
max_width = int(self.max_width.resolve(context))
|
max_width = int(self.max_width.resolve(context))
|
||||||
except VariableDoesNotExist:
|
except VariableDoesNotExist:
|
||||||
return ''
|
return ''
|
||||||
except ValueError:
|
except (ValueError, TypeError):
|
||||||
raise TemplateSyntaxError("widthratio final argument must be an number")
|
raise TemplateSyntaxError("widthratio final argument must be an number")
|
||||||
try:
|
try:
|
||||||
value = float(value)
|
value = float(value)
|
||||||
|
@ -456,7 +456,7 @@ class WidthRatioNode(Node):
|
||||||
ratio = (value / max_value) * max_width
|
ratio = (value / max_value) * max_width
|
||||||
except ZeroDivisionError:
|
except ZeroDivisionError:
|
||||||
return '0'
|
return '0'
|
||||||
except ValueError:
|
except (ValueError, TypeError):
|
||||||
return ''
|
return ''
|
||||||
return str(int(round(ratio)))
|
return str(int(round(ratio)))
|
||||||
|
|
||||||
|
|
|
@ -1466,6 +1466,14 @@ class Templates(unittest.TestCase):
|
||||||
# #10043: widthratio should allow max_width to be a variable
|
# #10043: widthratio should allow max_width to be a variable
|
||||||
'widthratio11': ('{% widthratio a b c %}', {'a':50,'b':100, 'c': 100}, '50'),
|
'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 ########################################################
|
### WITH TAG ########################################################
|
||||||
'with01': ('{% with key=dict.key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'),
|
'with01': ('{% with key=dict.key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'),
|
||||||
'legacywith01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'),
|
'legacywith01': ('{% with dict.key as key %}{{ key }}{% endwith %}', {'dict': {'key': 50}}, '50'),
|
||||||
|
|
Loading…
Reference in New Issue