django radioselect 渲染到表
我想将 django 表单小部件 radioselect 渲染到表中而不是 ul 列表中。标签位于第一行,单选按钮位于第二行下方。每个按钮一个单元格。例如,
-------------------------------
| label 1 | label 2 | label 3 |
-------------------------------
| O | O | O |
-------------------------------
我查看了默认的 selectradio 小部件,但渲染函数似乎很复杂,调用许多不同的类来完成渲染的每个部分。
有谁有如何执行此操作的示例或可以提供简单的解决方案吗?
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 |
-------------------------------
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只是为了补充一点 Béres Botond 的答案
自定义渲染器看起来像:
在这种情况下,我不想要旁边的单选框的名称,所以我使用“force_unicode(w.tag())”如果您想要它旁边的名称,只需直接渲染对象,如“force_unicode(w)”
我希望有帮助!
Just to fill in a bit more of Béres Botond's answer
The custom renderer would look like:
In this case I didn't want the name of the radio box next to it, so I am using "force_unicode(w.tag())" If you wanted the name next to it, just render the object directly like "force_unicode(w)"
I hope that helps!
您需要子类化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
您还可以使用 django-uni-form 并使用 div 而不是表格。
You could also use django-uni-form and use divs instead of tables.
如果您需要进一步自定义输入元素,请覆盖自定义渲染器上的 choice_input_class 属性。
这些
render()
和tag()
方法来自 1.9 源代码,仅稍加修改以显示每个方法中简单自定义的应用程序。If you need to customize the input elements further, overwrite the choice_input_class attribute on the custom renderer.
These
render()
andtag()
methods are from the 1.9 source code, modified only slightly to show the application of a simple customization in each.