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

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

    I can't believe several people have suggested using jQuery for this...

    Is it a case of: when the only tool you know is a hammer everything looks like a nail?

    Come on, if you're going to do it from the client-side (instead of fixing the source of the problem in the back-end code) surely the right place to do it would be in CSS?

    If you're in the admin site then it's a bit harder but if it's a regular page then it's easy to just omit the whole label from the form template, for example

    If you're in the admin site then you could still override the as_table, as_ul, as_p methods of BaseForm (see django/forms/forms.py) in your GalleryAdminForm class to omit the label of any field where the label is blank (or == ':' as the value may be at this stage of rendering)

    (...looking at lines 160-170 of forms.py it seems like Django 1.2 should properly omit the ':' if the label is blank so I guess you're on an older version?)

提交回复
热议问题