自定义RadioSelect
您好,我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 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