I would like to use jquery ui buttonset for a radioselect from a django form. I am using the custom renderer below for the radioselect widget, but the input lands inside the label. jquery ui doesn’t work with that structure. What do I need to change in the renderer to put the input tag before the label?
forms.py:
class SimpleRadioFieldRenderer(forms.widgets.RadioFieldRenderer):
def render(self):
"""Outputs widget without <ul> or <li> tags."""
return mark_safe(u'\n'.join([u'%s'
% force_unicode(w) for w in self]))
class MyForm(Form):
myradiofield = ChoiceField(
widget=RadioSelect(renderer=SimpleRadioFieldRenderer),
required=True,
choices=(('a','one choice'), ('b','another choice')))
Here is how django renders this in the template:
<label for="id_myradiofield_0"><input type="radio" name="myradiofield" value="a" id="id_myradiofield_0"> one choice</label>
<label for="id_myradiofield_1"><input type="radio" name="myradiofield" value="b" id="id_myradiofield_0"> another choice</label>
Here is how jquery ui expects it to look:
<input type="radio" name="myradiofield" value="a" id="id_myradiofield_0"><label for="id_myradiofield_0"> one choice</label>
<input type="radio" name="myradiofield" value="b" id="id_myradiofield_0"> <label for="id_myradiofield_1">another choice</label>
Thanks in advance for your help.
You can extend
RadioInputandRadioFieldRendererclasses for the issue and useMyCustomRendereras rendererIf you want a solution client side instead:
You can have the solution by modifying
django.forms.widgets.py:scroll to
class RadioInputwe will modify last line of
def __unicode__(self):goes to