How do I hide the field label for a HiddenInput widget in Django Admin?

前端 未结 10 1081
谎友^
谎友^ 2020-12-31 02:34

I\'ve got a bit of Django form code that looks like this:

class GalleryAdminForm(forms.ModelForm):
    auto_id=False
    order = forms.CharField(widget=forms         


        
10条回答
  •  感情败类
    2020-12-31 03:39

    The following removes the ':' from all your form fields. I've only tried it with the forms.Form class, but I believe it should work for forms.ModelForm too.

    In Django forms, the ':' after the labels is the label_suffix. You can change or remove the label_suffix by creating a subclass of ModelForm, here called UnstyledForm, and redefining the initialization function with label_suffix set to an empty string. Then use your new UnstyledForm class.

    class UnstyledForm(forms.ModelForm):
        def __init__(self, *args, **kwargs):
            kwargs.setdefault('label_suffix', '')
            super(UnstyledForm, self).__init__(*args, **kwargs)
    
    class GalleryAdminForm(UnstyledForm):
        auto_id=False
        order = forms.CharField(widget=forms.HiddenInput())
    

    I hope that helps!

提交回复
热议问题