Loading django/forms/widgets.py +2 −6 Original line number Diff line number Diff line Loading @@ -507,11 +507,7 @@ class CheckboxInput(Widget): def render(self, name, value, attrs=None): final_attrs = self.build_attrs(attrs, type='checkbox', name=name) try: result = self.check_test(value) except: # Silently catch exceptions result = False if result: if self.check_test(value): final_attrs['checked'] = 'checked' if not (value is True or value is False or value is None or value == ''): # Only add the 'value' attribute if a value is non-empty. Loading docs/ref/forms/widgets.txt +4 −0 Original line number Diff line number Diff line Loading @@ -310,6 +310,10 @@ commonly used groups of widgets: A callable that takes the value of the CheckBoxInput and returns ``True`` if the checkbox should be checked for that value. .. versionchanged:: 1.5 Exceptions from ``check_test`` used to be silenced by its caller, this is no longer the case, they will propagate upwards. ``Select`` ~~~~~~~~~~ Loading tests/regressiontests/forms/tests/widgets.py +3 −7 Original line number Diff line number Diff line Loading @@ -216,13 +216,9 @@ class FormsWidgetTestCase(TestCase): self.assertHTMLEqual(w.render('greeting', 'hello there'), '<input checked="checked" type="checkbox" name="greeting" value="hello there" />') self.assertHTMLEqual(w.render('greeting', 'hello & goodbye'), '<input checked="checked" type="checkbox" name="greeting" value="hello & goodbye" />') # A subtlety: If the 'check_test' argument cannot handle a value and raises any # exception during its __call__, then the exception will be swallowed and the box # will not be checked. In this example, the 'check_test' assumes the value has a # startswith() method, which fails for the values True, False and None. self.assertHTMLEqual(w.render('greeting', True), '<input type="checkbox" name="greeting" />') self.assertHTMLEqual(w.render('greeting', False), '<input type="checkbox" name="greeting" />') self.assertHTMLEqual(w.render('greeting', None), '<input type="checkbox" name="greeting" />') # Ticket #17888: calling check_test shouldn't swallow exceptions with self.assertRaises(AttributeError): w.render('greeting', True) # The CheckboxInput widget will return False if the key is not found in the data # dictionary (because HTML form submission doesn't send any result for unchecked Loading Loading
django/forms/widgets.py +2 −6 Original line number Diff line number Diff line Loading @@ -507,11 +507,7 @@ class CheckboxInput(Widget): def render(self, name, value, attrs=None): final_attrs = self.build_attrs(attrs, type='checkbox', name=name) try: result = self.check_test(value) except: # Silently catch exceptions result = False if result: if self.check_test(value): final_attrs['checked'] = 'checked' if not (value is True or value is False or value is None or value == ''): # Only add the 'value' attribute if a value is non-empty. Loading
docs/ref/forms/widgets.txt +4 −0 Original line number Diff line number Diff line Loading @@ -310,6 +310,10 @@ commonly used groups of widgets: A callable that takes the value of the CheckBoxInput and returns ``True`` if the checkbox should be checked for that value. .. versionchanged:: 1.5 Exceptions from ``check_test`` used to be silenced by its caller, this is no longer the case, they will propagate upwards. ``Select`` ~~~~~~~~~~ Loading
tests/regressiontests/forms/tests/widgets.py +3 −7 Original line number Diff line number Diff line Loading @@ -216,13 +216,9 @@ class FormsWidgetTestCase(TestCase): self.assertHTMLEqual(w.render('greeting', 'hello there'), '<input checked="checked" type="checkbox" name="greeting" value="hello there" />') self.assertHTMLEqual(w.render('greeting', 'hello & goodbye'), '<input checked="checked" type="checkbox" name="greeting" value="hello & goodbye" />') # A subtlety: If the 'check_test' argument cannot handle a value and raises any # exception during its __call__, then the exception will be swallowed and the box # will not be checked. In this example, the 'check_test' assumes the value has a # startswith() method, which fails for the values True, False and None. self.assertHTMLEqual(w.render('greeting', True), '<input type="checkbox" name="greeting" />') self.assertHTMLEqual(w.render('greeting', False), '<input type="checkbox" name="greeting" />') self.assertHTMLEqual(w.render('greeting', None), '<input type="checkbox" name="greeting" />') # Ticket #17888: calling check_test shouldn't swallow exceptions with self.assertRaises(AttributeError): w.render('greeting', True) # The CheckboxInput widget will return False if the key is not found in the data # dictionary (because HTML form submission doesn't send any result for unchecked Loading