diff --git a/django/forms/widgets.py b/django/forms/widgets.py index ec076398cc..224bd3de4d 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -481,9 +481,7 @@ class CheckboxInput(Widget): self.check_test = boolean_check if check_test is None else check_test def render(self, name, value, attrs=None): - final_attrs = self.build_attrs(attrs, type='checkbox', name=name) - if self.check_test(value): - final_attrs['checked'] = 'checked' + final_attrs = self.build_attrs(attrs, type='checkbox', name=name, checked=self.check_test(value)) 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. final_attrs['value'] = force_text(value) @@ -646,9 +644,13 @@ class ChoiceInput(SubWidget): def tag(self, attrs=None): attrs = attrs or self.attrs - final_attrs = dict(attrs, type=self.input_type, name=self.name, value=self.choice_value) - if self.is_checked(): - final_attrs['checked'] = 'checked' + final_attrs = dict( + attrs, + type=self.input_type, + name=self.name, + value=self.choice_value, + checked=self.is_checked(), + ) return format_html('', flatatt(final_attrs)) @property diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt index 9e9388344a..de66986e18 100644 --- a/docs/ref/forms/api.txt +++ b/docs/ref/forms/api.txt @@ -430,7 +430,7 @@ If the form is bound to data, the HTML output will include that data appropriately. For example, if a field is represented by an ````, the data will be in the ``value`` attribute. If a field is represented by an ````, then that HTML will -include ``checked="checked"`` if appropriate:: +include ``checked`` if appropriate:: >>> data = {'subject': 'hello', ... 'message': 'Hi there', @@ -441,7 +441,12 @@ include ``checked="checked"`` if appropriate::