Customizing RadioSelect

五迷三道 提交于 2019-12-09 13:30:04

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/1669845/customizing-radioselect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!