diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index 2c235513d06..62e2815d0b6 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -170,6 +170,13 @@ class CheckboxInput(Widget): final_attrs['value'] = force_unicode(value) # Only add the 'value' attribute if a value is non-empty. return u'' % flatatt(final_attrs) + def value_from_datadict(self, data, files, name): + if name not in data: + # A missing value means False because HTML form submission does not + # send results for unselected checkboxes. + return False + return super(CheckboxInput, self).value_from_datadict(data, files, name) + class Select(Widget): def __init__(self, attrs=None, choices=()): super(Select, self).__init__(attrs) diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py index 1c3e8739633..6b7c8580411 100644 --- a/tests/regressiontests/forms/widgets.py +++ b/tests/regressiontests/forms/widgets.py @@ -276,6 +276,12 @@ u'' >>> w.render('greeting', None) u'' +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 +checkboxes). +>>> w.value_from_datadict({}, {}, 'testing') +False + # Select Widget ############################################################### >>> w = Select()