summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2014-07-27 18:59:20 -0400
committerSimon Charette <charette.s@gmail.com>2014-07-28 10:01:07 -0400
commit80708ac77204c491f86c6a8215aaba8cf0989154 (patch)
tree38b7ab6899c2bb4f91fc263be69a01cef145e84e
parent729e4ae4f0730585ac4640e7fa3aa06374677ff2 (diff)
Fixed #23113 -- ChoiceInput.render should take specified attrs into account.
Thanks to Tim Graham for the review.
-rw-r--r--django/forms/widgets.py10
-rw-r--r--tests/forms_tests/tests/test_widgets.py6
2 files changed, 13 insertions, 3 deletions
diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index ee8ab5c129..a16b554385 100644
--- a/django/forms/widgets.py
+++ b/django/forms/widgets.py
@@ -601,13 +601,17 @@ class ChoiceInput(SubWidget):
label_for = format_html(' for="{0}"', self.id_for_label)
else:
label_for = ''
- return format_html('<label{0}>{1} {2}</label>', label_for, self.tag(), self.choice_label)
+ attrs = dict(self.attrs, **attrs) if attrs else self.attrs
+ return format_html(
+ '<label{0}>{1} {2}</label>', label_for, self.tag(attrs), self.choice_label
+ )
def is_checked(self):
return self.value == self.choice_value
- def tag(self):
- final_attrs = dict(self.attrs, type=self.input_type, name=self.name, value=self.choice_value)
+ def tag(self, attrs=None):
+ attrs = attrs or self.attrs
+ final_attrs = dict(attrs, type=self.input_type, name=self.name, value=self.choice_value)
if self.is_checked():
final_attrs['checked'] = 'checked'
return format_html('<input{0} />', flatatt(final_attrs))
diff --git a/tests/forms_tests/tests/test_widgets.py b/tests/forms_tests/tests/test_widgets.py
index ccd5316d16..1f19e98389 100644
--- a/tests/forms_tests/tests/test_widgets.py
+++ b/tests/forms_tests/tests/test_widgets.py
@@ -687,6 +687,12 @@ beatle J R Ringo False""")
self.assertFalse(r[1].is_checked())
self.assertEqual((r[1].name, r[1].value, r[1].choice_value, r[1].choice_label), ('beatle', 'J', 'P', 'Paul'))
+ # These individual widgets can accept extra attributes if manually rendered.
+ self.assertHTMLEqual(
+ r[1].render(attrs={'extra': 'value'}),
+ '<label><input type="radio" extra="value" name="beatle" value="P" /> Paul</label>'
+ )
+
with self.assertRaises(IndexError):
r[10]