4

我想要类似管理界面的东西。

这是表单的代码:

class NewRoleFrom(forms.Form):
    role = forms.ModelMultipleChoiceField(
        queryset=Role.objects.all(),
        widget=forms.CheckboxSelectMultiple
    )

所以,很简单,我有角色标签(角色:),然后数据库中的每个角色都用一个复选框呈现。像这样我可以取回用户选择的所有角色对象。但是在每行的开头我都有一个项目符号,我该如何删除它?list_display那么是否可以像我们在 中定义 a 时一样添加彼此的属性admin.py

4

3 回答 3

2

在您的表单模板中,只需遍历角色

表单.html

{% for role in form.role %}
    <div class="checkbox">
      {{ role }}
    </div>
{% endfor %}

然后就可以使用 css 了。

于 2013-12-05T20:30:32.817 回答
0

Here is the source for that widget from django.forms.widgets module :

class CheckboxSelectMultiple(SelectMultiple):
    def render(self, name, value, 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>']
        # 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))

    def id_for_label(self, id_):
        # See the comment for RadioSelect.id_for_label()
        if id_:
            id_ += '_0'
        return id_

You can see the bullets are due to django CSS for lists. So to remove them, think about creating a new wudget inheriting from CheckboxSelectMultiple with adding a class to the "ul" html tag, and then add your own css with the solution detailed here.

于 2013-04-11T10:25:46.460 回答
0

我会用自定义类覆盖 CheckboxSelectMultiple 类,并直接在渲染输出中插入样式更改。请参阅下面的代码

class CustomCheckboxSelectMultiple(forms.CheckboxSelectMultiple):
    def __init__(self, attrs=None):
        super(CustomCheckboxSelectMultiple, self).__init__(attrs)

    def render(self, name, value, attrs=None, choices=()):
        output = super(CustomCheckboxSelectMultiple, self).render(name, value, attrs, choices)

        style = self.attrs.get('style', None)
        if style:
            output = output.replace("<ul", format_html('<ul style="{0}"', style))

        return mark_safe(output)

然后以您的形式:

class NewRoleFrom(forms.Form):
    role = forms.ModelMultipleChoiceField(
        queryset=Role.objects.all(),
        widget=CustomCheckboxSelectMultiple(attrs={'style': 'list-style: none; margin: 0;'})
    )
于 2014-01-20T22:11:32.223 回答