Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.HiddenInput())

And that makes the form field go away, but it leaves the label "Order" in the Django admin page. If I use:

order = forms.CharField(widget=forms.HiddenInput(), label='')

I'm still left with the ":" between where the field and label used to be.

How do I hide the whole thing?!

like image 628
Gabriel Hurley Avatar asked Sep 11 '09 04:09

Gabriel Hurley


People also ask

How do I remove a label in Django?

In __init__ method set your field label as empty. This will remove label text.

How do I customize Modelan in Django?

To create ModelForm in django, you need to specify fields. Just associate the fields to first_name and lastName. Under the Meta class you can add : fields = ['first_name','lastName']. @Shishir solution works after I add that line. or you can try solution in Jihoon answers by adding vacant fields.

How do you make a field non editable in Django?

django-forms Using Model Form Making fields not editable Django 1.9 added the Field. disabled attribute: The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won't be editable by users.


2 Answers

Oraculum has got it right. You shouldn't be cleaning this up on the client side. If it is clutter, then you shouldn't be sending it to client at all. Building on Oraculum's answer, you should use a custom form template because you you probably still want the hidden values in the form.

{% for field in form.visible_fields %}
    <div>
        {{ field.errors }}
        <span class="filter-label">{{ field.label_tag }}</span><br>
        {{ field }}
    </div>
 {% endfor %}

 {% for field in form.hidden_fields %}
     <div style="display:none;">{{ field }}</div>
 {% endfor %}

Using a custom form template to control hidden fields is cleaner because it doesn't send extraneous info to the client.

like image 183
emispowder Avatar answered Oct 15 '22 05:10

emispowder


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?)

like image 34
Anentropic Avatar answered Oct 15 '22 05:10

Anentropic