From 50e299dbfbbfd796e63e7e13b4566cf69d2c4acb Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Thu, 21 Jul 2016 22:05:17 -0700 Subject: [PATCH] Fixed #26928 -- Changed forms' checked attribute to HTML5 boolean style. --- django/forms/widgets.py | 14 +++++++----- docs/ref/forms/api.txt | 21 +++++++++++++----- docs/ref/forms/widgets.txt | 4 ++++ docs/releases/1.11.txt | 3 +++ tests/forms_tests/tests/test_forms.py | 16 +++++++------- tests/forms_tests/tests/test_widgets.py | 22 +++++++++---------- .../widget_tests/test_checkboxinput.py | 14 ++++++------ .../test_checkboxselectmultiple.py | 18 +++++++-------- .../widget_tests/test_radioselect.py | 8 +++---- 9 files changed, 69 insertions(+), 51 deletions(-) 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:: - + + +.. versionchanged:: 1.11 + + The ``checked`` attribute was changed to use HTML5 boolean syntax rather + than ``checked="checked"``. This default output is a two-column HTML table, with a ```` for each field. Notice the following: @@ -471,6 +476,10 @@ Notice the following: attributes and ``