& lt; ul& gt;的Django设置类代替& lt; input& gt;与CheckboxSelectMultiple小部件 [英] Django Setting class of <ul> instead of <input> with CheckboxSelectMultiple widget

查看:51
本文介绍了& lt; ul& gt;的Django设置类代替& lt; input& gt;与CheckboxSelectMultiple小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以用如下一行来设置窗口小部件的类

I can set the class of a widget with a line like

self.widget = forms.CheckboxSelectMultiple(attrs={'class': 'myclass'})

,但这会将 myclass 应用于所有< li> 元素,例如

but this applies myclass to all of the <li> elements, e.g.,

<ul>
    <label><li><input type="checkbox" class="myclass">1</li></label>
    <label><li><input type="checkbox" class="myclass">2</li></label>
    <label><li><input type="checkbox" class="myclass">3</li></label>
</ul>

如何将类仅应用于< ul> 元素?

How can I apply a class to only the <ul> element?

推荐答案

默认情况下,您无法在UL标签上设置属性.简单"的事情是将CheckboxSelectMultiple子类化以完成您想要的事情.两种方法是:

By default you cannot set the attribute on the UL tag. The "easy" thing to do is subclass the CheckboxSelectMultiple to do what you want. Two approaches are:

使用"ulattrs"参数构造CheckboxSelectMultiple的自定义版本.

Construct a custom version of the CheckboxSelectMultiple with a "ulattrs" argument.

class MyCheckboxSelectMultiple(CheckboxSelectMultiple):
    def render(self, name, value, ulattrs=None, attrs=None, choices=()):
        if value is None: value = []
        has_id = attrs and 'id' in attrs
        final_attrs = self.build_attrs(attrs, name=name)
        output = [u'<ul class="%s">' % ulattrs.get('class')]
        # Normalize to strings
        str_values = set([force_unicode(v) for v in value])
        for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
            # If an ID attribute was given, add a numeric index as a suffix,
            # so that the checkboxes don't all have the same ID attribute.
            if has_id:
                final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
                label_for = u' for="%s"' % final_attrs['id']
            else:
                label_for = ''

            cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
            option_value = force_unicode(option_value)
            rendered_cb = cb.render(name, option_value)
            option_label = conditional_escape(force_unicode(option_label))
            output.append(u'<li><label%s>%s %s</label></li>' % (label_for, rendered_cb, option_label))
        output.append(u'</ul>')
        return mark_safe(u'\n'.join(output))

或者,您可以做一些更怪诞的事情...

or, you could do something a little more hacky...

class MyCheckboxSelectMultiple(CheckboxSelectMultiple):
    def render(self, name, value, attrs=None, choices=()):
        html = super(MyCheckboxSelectMultiple, self).render(name, value, attrs, choices)

        return mark_safe(html.replace('<ul>', '<ul class="foobar">'))

这篇关于&amp; lt; ul&amp; gt;的Django设置类代替&amp; lt; input&amp; gt;与CheckboxSelectMultiple小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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