Customizing RadioSelect
Asked Answered
O

1

3

Hello I have a form with ChoiceField whose widget is set to RadioSelect

Now to override default html output one needs to subclass RadioFieldRenderer like this:

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.tag()) for w in self]))

All is good now except I'd like to be able to render 1 radio button at a time from template. Something like this:

{{ form.myfield.0 }}}

Or perhaps hanging it onto widget itself:

{{ form.myfield.field.widget.0 }}

What bugs me is that RadioFieldRenderer already implements __get_item__ to get just one RadioInput. The problem is that the renderer does not contain data, neither does the widget. And I'd really hate to mess with Field and BoundField.

I need this to inject html before/after each radiobutton and I'd like it to be done in the template. From the code it would be easy.

Any ideas are welcome.

Occasional answered 3/11, 2009 at 20:8 Comment(0)
M
0

I think this post in django-users may provide a way to do it (with an accessor function in the form class): http://groups.google.com/group/django-users/msg/b60410692c7c60e2

Mathias answered 5/2, 2010 at 11:59 Comment(1)
I will try this, when I get the chance.Occasional

© 2022 - 2024 — McMap. All rights reserved.