Fixed #17114 -- Handled integer values 0 and 1 for checkboxes like other integer values, not like False and True.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17132 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
0a272e4201
commit
19e54084dc
|
@ -464,10 +464,13 @@ class TimeInput(Input):
|
||||||
return super(TimeInput, self)._has_changed(self._format_value(initial), data)
|
return super(TimeInput, self)._has_changed(self._format_value(initial), data)
|
||||||
|
|
||||||
class CheckboxInput(Widget):
|
class CheckboxInput(Widget):
|
||||||
def __init__(self, attrs=None, check_test=bool):
|
def __init__(self, attrs=None, check_test=None):
|
||||||
super(CheckboxInput, self).__init__(attrs)
|
super(CheckboxInput, self).__init__(attrs)
|
||||||
# check_test is a callable that takes a value and returns True
|
# check_test is a callable that takes a value and returns True
|
||||||
# if the checkbox should be checked for that value.
|
# if the checkbox should be checked for that value.
|
||||||
|
if check_test is None:
|
||||||
|
self.check_test = lambda v: not (v is False or v is None or v == '')
|
||||||
|
else:
|
||||||
self.check_test = check_test
|
self.check_test = check_test
|
||||||
|
|
||||||
def render(self, name, value, attrs=None):
|
def render(self, name, value, attrs=None):
|
||||||
|
@ -478,7 +481,7 @@ class CheckboxInput(Widget):
|
||||||
result = False
|
result = False
|
||||||
if result:
|
if result:
|
||||||
final_attrs['checked'] = 'checked'
|
final_attrs['checked'] = 'checked'
|
||||||
if value not in ('', True, False, None):
|
if not (value is True or value is False or value is None or value == ''):
|
||||||
# Only add the 'value' attribute if a value is non-empty.
|
# Only add the 'value' attribute if a value is non-empty.
|
||||||
final_attrs['value'] = force_unicode(value)
|
final_attrs['value'] = force_unicode(value)
|
||||||
return mark_safe(u'<input%s />' % flatatt(final_attrs))
|
return mark_safe(u'<input%s />' % flatatt(final_attrs))
|
||||||
|
|
|
@ -195,6 +195,10 @@ class FormsWidgetTestCase(TestCase):
|
||||||
|
|
||||||
self.assertEqual(w.render('is_cool', False, attrs={'class': 'pretty'}), u'<input type="checkbox" name="is_cool" class="pretty" />')
|
self.assertEqual(w.render('is_cool', False, attrs={'class': 'pretty'}), u'<input type="checkbox" name="is_cool" class="pretty" />')
|
||||||
|
|
||||||
|
# regression for #17114
|
||||||
|
self.assertEqual(w.render('is_cool', 0), u'<input checked="checked" type="checkbox" name="is_cool" value="0" />')
|
||||||
|
self.assertEqual(w.render('is_cool', 1), u'<input checked="checked" type="checkbox" name="is_cool" value="1" />')
|
||||||
|
|
||||||
# You can also pass 'attrs' to the constructor:
|
# You can also pass 'attrs' to the constructor:
|
||||||
w = CheckboxInput(attrs={'class': 'pretty'})
|
w = CheckboxInput(attrs={'class': 'pretty'})
|
||||||
self.assertEqual(w.render('is_cool', ''), u'<input type="checkbox" class="pretty" name="is_cool" />')
|
self.assertEqual(w.render('is_cool', ''), u'<input type="checkbox" class="pretty" name="is_cool" />')
|
||||||
|
|
Loading…
Reference in New Issue