The problem with Django's default Radio button widget is that it puts the buttons in a vertical format and the documentation is somewhat silent on how to make them horizontal. If you are dealing with a long list of buttons then veritical is probably the way to go but if you are dealing with "YES/NO" situation or any other boolean choice then you will probably want them horizontal.
Basically I just took the code and even the explanation from here:
https://wikis.utexas.edu/display/~bm6432/Django-Modifying+RadioSelect+Widget+to+have+horizontal+buttons
1 2 3 4 5 6 7 8 9 10 11 12 | from django.forms.widgets import RadioSelect
class HorizRadioRenderer(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 HorizRadioSelect(RadioSelect):
renderer = HorizRadioRenderer
|
More like this
- Form field with fixed value by roam 3 days, 18 hours ago
- New Snippet! by Antoliny0919 1 week, 3 days ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 2 months, 4 weeks ago
- get_object_or_none by azwdevops 6 months, 3 weeks ago
- Mask sensitive data from logger by agusmakmun 8 months, 2 weeks ago
Comments
Please login first before commenting.