自定义RadioSelect

发布于 2024-08-09 16:11:21 字数 778 浏览 2 评论 0原文

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

Now 以覆盖默认的 html 输出,需要像这样子类化 RadioFieldRenderer:

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 个单选按钮。 像这样的事情:

{{ form.myfield.0 }}}

或者也许将它挂在小部件本身上:

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

令我烦恼的是 RadioFieldRenderer 已经实现了 __get_item__ 来获取一个 RadioInput。问题是渲染器不包含数据,小部件也不包含数据。我真的不想乱搞 Field 和 BoundField。

我需要在每个单选按钮之前/之后注入 html,并且我希望它在模板中完成。从代码来看,这很容易。

欢迎任何想法。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你的背包 2024-08-16 16:11:21

我认为 django-users 中的这篇文章可能提供了一种方法(使用表单类中的访问器函数):
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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文