customize radio buttons in django
Sounds like a job for a custom widget renderer:
from django.utils.safestring import mark_safe
class HorizRadioRenderer(forms.RadioSelect.renderer):
""" this overrides widget method to put radio buttons horizontally
instead of vertically.
"""
def render(self):
"""Outputs radios"""
return mark_safe(u'\n'.join([u'%s\n' % w for w in self]))
class IncidentForm(forms.ModelForm):
incident_live = forms.ChoiceField(widget=forms.RadioSelect(renderer=HorizRadioRenderer),choices=INCIDENT_LIVE)
taken from https://wikis.utexas.edu/display/~bm6432/Django-Modifying+RadioSelect+Widget+to+have+horizontal+buttons
This solution of "alecxe" didn't actually worked for me. But Adding CSS code did work:
{% block style %}
<style>
.radio{
display:inline-block;
}
</style>
{% endblock %}
Actually the solution should be quite straight forward. It's all there in Django documentation.
Here is what I have done in some of the forms:
In the template, where the form fields are being laid out, I will do this:
{% for field in form.visible_fields %}
{{ field.errors }}
{% if field.name == "<some_field_name>" %}
...
...
{% elif field.name == "<radio_field_name>" %}
{% for radio in field %}
<label for="{{ radio.id_for_label }}">
{{ radio.choice_label }}
<span class="radio">{{ radio.tag }}</span>
</label>
{% endfor %}
{% endif %}
{% endfor %}
The resultant radio selects:
And this has worked thus far.