django radioselect渲染到表格 [英] django radioselect render to table

查看:96
本文介绍了django radioselect渲染到表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将Django表单窗口小部件radioselect渲染到表中而不是ul列表中.第一行带有标签,第二行带有下方的单选按钮.每个按钮一个单元格.例如

I want to render the django form widget radioselect into a table rather than a ul list. With labels in the first row and the radio buttons below in the second row. One cell for each button. e.g.

-------------------------------
| label 1 | label 2 | label 3 |
-------------------------------
|   O     |    O    |    O    |
-------------------------------

我已经看过默认的selectradio小部件,但是render函数似乎太复杂了,调用许多不同的类来完成渲染的每个部分.

I've looked at the default selectradio widget but the render function seems so complicated, calling many different classes to do each part of the render.

有没有人举任何例子,或者可以提供一个简单的解决方案?

Does anyone have any examples of how to do this or could provide a simple solution?

推荐答案

您需要将 django.forms.widgets.RadioFieldRenderer 子类化,并覆盖它的 render 方法. 然后在表单中,在声明字段时为小部件指定自定义渲染器

You need to subclass django.forms.widgets.RadioFieldRenderer and override it's render method. Then in your form, when declaring your field specify the custom renderer for the widget

class MyForm(forms.ModelForm):
    my_field = forms.TypedChoiceField(choices=some_choices,
                                      label=u"bla",
                                      widget=forms.RadioSelect(renderer=MyCustomRenderer))

这篇关于django radioselect渲染到表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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