Ticket #9336: checkbox_false.diff
File checkbox_false.diff, 1.8 KB (added by , 16 years ago) |
---|
-
django/forms/widgets.py
314 314 return super(TimeInput, self).render(name, value, attrs) 315 315 316 316 class CheckboxInput(Widget): 317 def __init__(self, attrs=None, check_test= bool):317 def __init__(self, attrs=None, check_test=lambda s: s != 'False' and bool(s)): 318 318 super(CheckboxInput, self).__init__(attrs) 319 319 # check_test is a callable that takes a value and returns True 320 320 # if the checkbox should be checked for that value. … … 328 328 result = False 329 329 if result: 330 330 final_attrs['checked'] = 'checked' 331 if value not in ('', True, False, None):331 if value not in ('', True, False, 'True', 'False', None): 332 332 # Only add the 'value' attribute if a value is non-empty. 333 333 final_attrs['value'] = force_unicode(value) 334 334 return mark_safe(u'<input%s />' % flatatt(final_attrs)) -
tests/regressiontests/forms/widgets.py
279 279 >>> w.render('is_cool', 'foo') 280 280 u'<input checked="checked" type="checkbox" name="is_cool" value="foo" />' 281 281 282 A value of 'False' should be rendered unchecked 283 >>> w.render('is_cool', 'False') 284 u'<input type="checkbox" name="is_cool" />' 285 286 'True' should be rendered without a value attribute 287 >>> w.render('is_cool', 'True') 288 u'<input checked="checked" type="checkbox" name="is_cool" />' 289 282 290 >>> w.render('is_cool', False, attrs={'class': 'pretty'}) 283 291 u'<input type="checkbox" name="is_cool" class="pretty" />' 284 292