Skip to content

Commit d75e23c

Browse files
committed
[1.1.X] Fixed #9336. Changed CheckboxInput to render 'True' and 'False' input strings as checked or not instead of as a value attribute. Backport of r12556 from trunk.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12557 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 778b7bc commit d75e23c

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

django/forms/widgets.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,12 @@ def value_from_datadict(self, data, files, name):
384384
# A missing value means False because HTML form submission does not
385385
# send results for unselected checkboxes.
386386
return False
387-
return super(CheckboxInput, self).value_from_datadict(data, files, name)
387+
value = data.get(name)
388+
# Translate true and false strings to boolean values.
389+
values = {'true': True, 'false': False}
390+
if isinstance(value, basestring):
391+
value = values.get(value.lower(), value)
392+
return value
388393

389394
def _has_changed(self, initial, data):
390395
# Sometimes data or initial could be None or u'' which should be the

tests/regressiontests/forms/forms.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,24 @@
295295
>>> print f['get_spam']
296296
<input checked="checked" type="checkbox" name="get_spam" />
297297
298+
'True' or 'true' should be rendered without a value attribute
299+
>>> f = SignupForm({'email': '[email protected]', 'get_spam': 'True'}, auto_id=False)
300+
>>> print f['get_spam']
301+
<input checked="checked" type="checkbox" name="get_spam" />
302+
303+
>>> f = SignupForm({'email': '[email protected]', 'get_spam': 'true'}, auto_id=False)
304+
>>> print f['get_spam']
305+
<input checked="checked" type="checkbox" name="get_spam" />
306+
307+
A value of 'False' or 'false' should be rendered unchecked
308+
>>> f = SignupForm({'email': '[email protected]', 'get_spam': 'False'}, auto_id=False)
309+
>>> print f['get_spam']
310+
<input type="checkbox" name="get_spam" />
311+
312+
>>> f = SignupForm({'email': '[email protected]', 'get_spam': 'false'}, auto_id=False)
313+
>>> print f['get_spam']
314+
<input type="checkbox" name="get_spam" />
315+
298316
Any Field can have a Widget class passed to its constructor:
299317
>>> class ContactForm(Form):
300318
... subject = CharField()

0 commit comments

Comments
 (0)