【问题标题】:How to remove label?如何去除标签?
【发布时间】:2017-09-21 10:31:31
【问题描述】:

如何从模板中的MultipleChoiceField中删除标签?

forms.py:

class RequirementAddForm(forms.ModelForm):
    symbol = forms.MultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple, choices=REQUIREMENTS_CHOICES,)

    class Meta:
        model = Requirement
        fields = ('symbol',)

template.html:

{{ form }}

【问题讨论】:

    标签: python html django python-3.x django-1.10


    【解决方案1】:

    这里有一些可能的解决方案

    1) 在表单定义中使用label=""

    2) 覆盖标签,如果您使用的是继承的表单并且没有直接访问权限

    def __init__(self, *args, **kwargs):
        super(FormClass, self).__init__(*args, **kwargs)
        self.fields['field'].label = ''
    

    另一种可能的选择是将auto_id=False 传递到表单中

    【讨论】:

    • 谢谢!一开始我使用self.fields['symbol'].label = None,但它没有用。然后在你回答之后我使用self.fields['field'].label = '' 它对我有用。
    猜你喜欢
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 2012-10-19
    • 2019-06-02
    • 1970-01-01
    相关资源
    最近更新 更多