自定义RadioSelect [英] Customizing RadioSelect

查看:181
本文介绍了自定义RadioSelect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我有一个表单与ChoiceField其小部件设置为RadioSelect

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

现在要覆盖默认的html输出,需要像这样子类RadioFieldRenderer:

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]))

现在除了我想要的一切都好从模板一次渲染1个单选按钮。
这样的东西:

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 }}

我的错误是RadioFieldRenderer已经实现了 __ get_item __ 来获取一个RadioInput。问题是渲染器不包含数据,这个小部件也不是。我真的很讨厌混淆Field和BoundField。

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.

我需要这个在每个单选按钮之前/之后注入html,我希望在模板。

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.

推荐答案

我认为django-user中的这篇文章可能会提供一种方法(在表单类中有一个访问器函数):
http://groups.google.com/group/django-users/msg/b60410692c7c60e2

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

这篇关于自定义RadioSelect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆