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

前端 未结 10 1108
谎友^
谎友^ 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:28

    If you're using JQuery this should do the trick:

    Your form

    TO_HIDE_ATTRS = {'class': 'hidden'}
    class GalleryAdminForm(forms.ModelForm):
        auto_id=False
        order = forms.CharField(widget=forms.TextInput(attrs=TO_HIDE_ATTRS))
    

    Javascript code to add to your template

    $(document).ready(function(){
        $('tr:has(.hidden)').hide();
    });
    

    That works if you're rendering your form as a table. If you want to make it work with any kind of form rendering you can do as follows:

    $(document).ready(function(){
        $('{{ form_field_container }}:has(.hidden)').hide();
    });
    

    And add form_field_container to your template context. An example:

    If you render your form like this:

        
    {{ field.label_tag }} {{ field }}

    Your context must include:

    'form_field_container': 'span'
    

    You get the idea...

提交回复
热议问题