Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #9336. Changed CheckboxInput to render 'True' and 'False' input…

… strings as checked or not instead of as a value attribute. Thanks, bthomas.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12556 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 445e0a5c3e40dbcfd866b4594371b0fa49fdc223 1 parent d47b9ec
@jkocherhans jkocherhans authored
Showing with 24 additions and 1 deletion.
  1. +6 −1 django/forms/widgets.py
  2. +18 −0 tests/regressiontests/forms/forms.py
View
7 django/forms/widgets.py
@@ -382,7 +382,12 @@ def value_from_datadict(self, data, files, name):
# 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)
+ value = data.get(name)
+ # Translate true and false strings to boolean values.
+ values = {'true': True, 'false': False}
+ if isinstance(value, basestring):
+ value = values.get(value.lower(), value)
+ return value
def _has_changed(self, initial, data):
# Sometimes data or initial could be None or u'' which should be the
View
18 tests/regressiontests/forms/forms.py
@@ -295,6 +295,24 @@
>>> print f['get_spam']
<input checked="checked" type="checkbox" name="get_spam" />
+'True' or 'true' should be rendered without a value attribute
+>>> f = SignupForm({'email': 'test@example.com', 'get_spam': 'True'}, auto_id=False)
+>>> print f['get_spam']
+<input checked="checked" type="checkbox" name="get_spam" />
+
+>>> f = SignupForm({'email': 'test@example.com', 'get_spam': 'true'}, auto_id=False)
+>>> print f['get_spam']
+<input checked="checked" type="checkbox" name="get_spam" />
+
+A value of 'False' or 'false' should be rendered unchecked
+>>> f = SignupForm({'email': 'test@example.com', 'get_spam': 'False'}, auto_id=False)
+>>> print f['get_spam']
+<input type="checkbox" name="get_spam" />
+
+>>> f = SignupForm({'email': 'test@example.com', 'get_spam': 'false'}, auto_id=False)
+>>> print f['get_spam']
+<input type="checkbox" name="get_spam" />
+
Any Field can have a Widget class passed to its constructor:
>>> class ContactForm(Form):
... subject = CharField()
Please sign in to comment.
Something went wrong with that request. Please try again.