I’m rendering a TypedChoiceField, with a RadioSelect widget. Setting attrs={'class':'radio'} for the widget, this class is applied to the <input>, but this input is nested in a <ul>. I would like to set a class for this <ul>, where should I do so?
I am currently still on Django 1.3, so a RadioSelect isn’t an option.
From my Form:
yesno = forms.TypedChoiceField(
coerce=lambda x: True if x == 'Yes' else False,
choices=((False, 'No'), (True, 'Yes')),
widget=forms.RadioSelect)
The actual HTML:
<ul>
<li>
<label for="yesno_0">
<input value="False" type="radio" name="yesno" id="yesno_0">
No
</label>
</li>
<li>
<label for="yesno_1">
<input name="yesno" value="True" id="yesno_1" type="radio">
Yes
</label>
</li>
</ul>
What I’d like:
<ul class='myclass'>
<li>
<label for="yesno_0">
...
The simplest way i see:
You could create a custom RadioSelect widget, inherited from the widgets.RadioSelect and override the “renderer” property and set it to your custom RadioFieldRenderer (also inherited from widgets.RadioFieldRenderer) and override the “render” method
Or call
renderwith asuper()and then replace<ul>with a<ul class=''>, that’s a detail