Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17114 -- Handled integer values 0 and 1 for checkboxes like ot…

…her integer values, not like False and True.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17132 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 19e54084dc7d85d0bf190e0d79575126c5d29a50 1 parent 0a272e4
Aymeric Augustin authored November 21, 2011
9  django/forms/widgets.py
@@ -464,11 +464,14 @@ def _has_changed(self, initial, data):
464 464
         return super(TimeInput, self)._has_changed(self._format_value(initial), data)
465 465
 
466 466
 class CheckboxInput(Widget):
467  
-    def __init__(self, attrs=None, check_test=bool):
  467
+    def __init__(self, attrs=None, check_test=None):
468 468
         super(CheckboxInput, self).__init__(attrs)
469 469
         # check_test is a callable that takes a value and returns True
470 470
         # if the checkbox should be checked for that value.
471  
-        self.check_test = check_test
  471
+        if check_test is None:
  472
+            self.check_test = lambda v: not (v is False or v is None or v == '')
  473
+        else:
  474
+            self.check_test = check_test
472 475
 
473 476
     def render(self, name, value, attrs=None):
474 477
         final_attrs = self.build_attrs(attrs, type='checkbox', name=name)
@@ -478,7 +481,7 @@ def render(self, name, value, attrs=None):
478 481
             result = False
479 482
         if result:
480 483
             final_attrs['checked'] = 'checked'
481  
-        if value not in ('', True, False, None):
  484
+        if not (value is True or value is False or value is None or value == ''):
482 485
             # Only add the 'value' attribute if a value is non-empty.
483 486
             final_attrs['value'] = force_unicode(value)
484 487
         return mark_safe(u'<input%s />' % flatatt(final_attrs))
4  tests/regressiontests/forms/tests/widgets.py
@@ -195,6 +195,10 @@ def test_checkboxinput(self):
195 195
 
196 196
         self.assertEqual(w.render('is_cool', False, attrs={'class': 'pretty'}), u'<input type="checkbox" name="is_cool" class="pretty" />')
197 197
 
  198
+        # regression for #17114
  199
+        self.assertEqual(w.render('is_cool', 0), u'<input checked="checked" type="checkbox" name="is_cool" value="0" />')
  200
+        self.assertEqual(w.render('is_cool', 1), u'<input checked="checked" type="checkbox" name="is_cool" value="1" />')
  201
+
198 202
         # You can also pass 'attrs' to the constructor:
199 203
         w = CheckboxInput(attrs={'class': 'pretty'})
200 204
         self.assertEqual(w.render('is_cool', ''), u'<input type="checkbox" class="pretty" name="is_cool" />')

0 notes on commit 19e5408

Please sign in to comment.
Something went wrong with that request. Please try again.