Django Setting class of
    instead of <input&

2020-07-18 09:25发布

问题:

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

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

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>

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

回答1:

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:

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">'))


回答2:

Building on Koblas, another somewhat more general approach is:

class CheckboxSelectMultipleULAttrs(forms.CheckboxSelectMultiple):
    """
    Class to allow setting attributes on containing ul in a CheckboxSelectMultiple
    """
    def __init__(self, ulattrs=None, attrs=None, choices=()):
        self.ulattrs = ulattrs
        super(CheckboxSelectMultipleULAttrs, self).__init__(attrs, choices)
        return

    def render(self, name, value, attrs=None, choices=()):
        html = super(CheckboxSelectMultipleULAttrs, self).render(name, value, attrs, choices)
        if not self.ulattrs:
            return html
        return mark_safe(html.replace('<ul>', '<ul ' + self.ulattrs + '>'))