mirror of https://github.com/django/django.git
Fixed #31073 -- Prevented CheckboxInput.get_context() from mutating attrs.
This commit is contained in:
parent
3fb7c12158
commit
02eff7ef60
|
@ -522,9 +522,7 @@ class CheckboxInput(Input):
|
||||||
|
|
||||||
def get_context(self, name, value, attrs):
|
def get_context(self, name, value, attrs):
|
||||||
if self.check_test(value):
|
if self.check_test(value):
|
||||||
if attrs is None:
|
attrs = {**(attrs or {}), 'checked': True}
|
||||||
attrs = {}
|
|
||||||
attrs['checked'] = True
|
|
||||||
return super().get_context(name, value, attrs)
|
return super().get_context(name, value, attrs)
|
||||||
|
|
||||||
def value_from_datadict(self, data, files, name):
|
def value_from_datadict(self, data, files, name):
|
||||||
|
|
|
@ -89,3 +89,8 @@ class CheckboxInputTest(WidgetTest):
|
||||||
def test_value_omitted_from_data(self):
|
def test_value_omitted_from_data(self):
|
||||||
self.assertIs(self.widget.value_omitted_from_data({'field': 'value'}, {}, 'field'), False)
|
self.assertIs(self.widget.value_omitted_from_data({'field': 'value'}, {}, 'field'), False)
|
||||||
self.assertIs(self.widget.value_omitted_from_data({}, {}, 'field'), False)
|
self.assertIs(self.widget.value_omitted_from_data({}, {}, 'field'), False)
|
||||||
|
|
||||||
|
def test_get_context_does_not_mutate_attrs(self):
|
||||||
|
attrs = {'checked': False}
|
||||||
|
self.widget.get_context('name', True, attrs)
|
||||||
|
self.assertIs(attrs['checked'], False)
|
||||||
|
|
|
@ -1103,6 +1103,17 @@ class TestSplitFormWidget(PostgreSQLWidgetTestCase):
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_checkbox_get_context_attrs(self):
|
||||||
|
context = SplitArrayWidget(
|
||||||
|
forms.CheckboxInput(),
|
||||||
|
size=2,
|
||||||
|
).get_context('name', [True, False])
|
||||||
|
self.assertEqual(context['widget']['value'], '[True, False]')
|
||||||
|
self.assertEqual(
|
||||||
|
[subwidget['attrs'] for subwidget in context['widget']['subwidgets']],
|
||||||
|
[{'checked': True}, {}]
|
||||||
|
)
|
||||||
|
|
||||||
def test_render(self):
|
def test_render(self):
|
||||||
self.check_html(
|
self.check_html(
|
||||||
SplitArrayWidget(forms.TextInput(), size=2), 'array', None,
|
SplitArrayWidget(forms.TextInput(), size=2), 'array', None,
|
||||||
|
|
Loading…
Reference in New Issue