diff --git a/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select.html b/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select.html index cd45e8ab..584f969f 100644 --- a/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select.html +++ b/src/django_bootstrap5/templates/django_bootstrap5/widgets/radio_select.html @@ -12,6 +12,7 @@ id="{{ option.attrs.id }}" {% if option.value != None %} value="{{ option.value|stringformat:'s' }}" {% if option.attrs.checked %} checked="checked"{% endif %}{% endif %} + {% if widget.attrs.form %} form="{{ widget.attrs.form }}{% endif %} {% if widget.attrs.disabled or option.attrs.disabled %} disabled{% endif %}> diff --git a/tests/test_bootstrap_field_radio_select.py b/tests/test_bootstrap_field_radio_select.py index 3e28f380..088c663e 100644 --- a/tests/test_bootstrap_field_radio_select.py +++ b/tests/test_bootstrap_field_radio_select.py @@ -22,7 +22,16 @@ class DisabledSelectTestForm(forms.Form): widget=forms.RadioSelect, disabled=True, ) + +class SelectOtherTestForm(forms.Form): + test = forms.ChoiceField( + choices=( + (1, "one"), + (2, "two"), + ), + widget=forms.RadioSelect(attrs={"form": "another-form"}) + ) class RadioSelectWithDisabledOptions(forms.RadioSelect): @@ -136,6 +145,28 @@ def test_disabled_select(self): "" ), ) + + def test_other_form_select(self): + """Test field with select that belongs to another form widget.""" + self.maxDiff = None + self.assertHTMLEqual( + self.render("{% bootstrap_field form.test %}", context={"form": DisabledSelectTestForm()}), + ( + '
' + '' + '
' + '
' + '' + '' + "
" + '
' + '' + '' + "
" + "
" + "
" + ), + ) def test_single_disabled_option(self): """Test field with a disabled option."""