django RadioSelect小部件列表ID [英] django RadioSelect widget list id

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

问题描述

我目前要在CSS样式中设置一个窗体类上有一个RadioSelect小部件.但是,呈现的窗口小部件包含在 ul 中,并且列表没有ID.

I currently have a RadioSelect widget on one of my form classes that I want to style with css. However the rendered widget is contained in an ul and the list has no id.

将ID添加到呈现列表的最简单方法是什么?

What is the easiest way to add an id to the rendered list?

推荐答案

子类 RadioFieldRenderer 并覆盖 render 方法.

RadioFieldRenderer 是RadioSelect用来自定义无线电窗口小部件的对象.

RadioFieldRenderer is an object used by RadioSelect to enable customization of radio widgets.

将类添加到 ul 元素的示例实现:

Example implementation that adds class to ul element:

from django.utils.encoding import force_unicode
from django.utils.safestring import mark_safe

class MyRadioFieldRenderer(forms.widgets.RadioFieldRenderer):

    def render(self):
        """Outputs a <ul> for this set of radio fields."""
        return mark_safe(u'<ul class="inputs-list">\n%s\n</ul>' % 
                u'\n'.join([u'<li>%s</li>'
                % force_unicode(w) for w in self]))

用法:

field = forms.ChoiceField(label=_('field'),
        choices=FIELD_CHOICES,
        widget=forms.widgets.RadioSelect(
            renderer=MyRadioFieldRenderer
            )
        )

这篇关于django RadioSelect小部件列表ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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