[ACCEPTED]-How do I hide the field label for a HiddenInput widget in Django Admin?-django-admin

Accepted answer
Score: 43

Oraculum has got it right. You shouldn't 9 be cleaning this up on the client side. If 8 it is clutter, then you shouldn't be sending 7 it to client at all. Building on Oraculum's 6 answer, you should use a custom form template 5 because you you probably still want the 4 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 3 form template to control hidden fields is 2 cleaner because it doesn't send extraneous 1 info to the client.

Score: 37

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

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

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

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

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

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

Score: 20

Try

{% for field in form.visible_fields %}

0

Score: 3

I think it's simpler to achieve the ":" label 2 omission for HiddenInput widget by modifying 1 class AdminField(object) in contrib/admin/helpers.py from :

    if self.is_checkbox:
        classes.append(u'vCheckboxLabel')
        contents = force_unicode(escape(self.field.label))
    else:
        contents = force_unicode(escape(self.field.label)) + u':'

to :

    if self.is_checkbox:
        classes.append(u'vCheckboxLabel')
        contents = force_unicode(escape(self.field.label))
    else:            
        contents = force_unicode(escape(self.field.label))
        #MODIFIED 26/10/2009
        if self.field.label <> '':
           contents += u':'
        # END MODIFY
Score: 3

Check the answer at Create a hidden field in the admin site, it can be done without 2 JavaScript by overriding admin/includes/fieldset.html From there, you 1 can inject a CSS class, and do the rest.

Score: 2

In theory, you should be able to pass label_suffix into 8 the form constructor. However, the Django 7 admin ignores this.

You've been bitten by 6 two bugs in Django: #18134 'BoundField.label_tag should include form.label_suffix' (fixed in trunk, should 5 be in 1.6) and to a lesser extent #11277 Hidden fields in Inlines are displayed as empty rows.

Currently, the 4 best solution is to override the admin fieldset 3 template. Use a HiddenInput for your widget, then override 2 the admin fieldset template (documented here). Just create 1 a templates/admin/includes/fieldset.html with the following contents:

<fieldset class="module aligned {{ fieldset.classes }}">
    {% if fieldset.name %}<h2>{{ fieldset.name }}</h2>{% endif %}
    {% if fieldset.description %}
        <div class="description">{{ fieldset.description|safe }}</div>
    {% endif %}
    {% for line in fieldset %}
        <div class="form-row{% if line.fields|length_is:'1' and line.errors %} errors{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}">
            {% if line.fields|length_is:'1' %}{{ line.errors }}{% endif %}
            {% for field in line %}
                <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}"{% endif %}>
                    {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
                    {% if field.is_checkbox %}
                        {{ field.field }}{{ field.label_tag }}
                    {% else %}
                        {# only show the label for visible fields #}
                        {% if not field.field.is_hidden %}
                        {{ field.label_tag }}
                        {% endif %}

                        {% if field.is_readonly %}
                            <p>{{ field.contents }}</p>
                        {% else %}
                            {{ field.field }}
                        {% endif %}
                    {% endif %}
                    {% if field.field.help_text %}
                        <p class="help">{{ field.field.help_text|safe }}</p>
                    {% endif %}
                </div>
            {% endfor %}
        </div>
    {% endfor %}
</fieldset>
Score: 2

Based upon the solution by Wilfried Hughes 7 I 've changed the fieldset.html with little 6 improvements.

The code snippet below not 5 only hides the input element instead if 4 the fieldset contains only one single element 3 which input-type is set to hidden it also 2 hides the surrounding div-elements wasting 1 no space in the form.

<fieldset class="module aligned {{ fieldset.classes }}">
{% if fieldset.name %}<h2>{{ fieldset.name }}</h2>{% endif %}
{% if fieldset.description %}
    <div class="description">{{ fieldset.description|safe }}</div>
{% endif %}
{% for line in fieldset %}
    <div class="form-row{% if line.fields|length_is:'1' and line.errors %} errors{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}"{% if line.fields|length_is:'1' %}{% for field in line %}{% if field.field.is_hidden %} style="display: none"{% endif %}{% endfor %}{% endif %}>
        {% if line.fields|length_is:'1' %}{{ line.errors }}{% endif %}
        {% for field in line %}
            <div{% if not line.fields|length_is:'1' %} class="field-box{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% if not field.is_readonly and field.errors %} errors{% endif %}"{% endif %}{% if field.field.is_hidden %} style="display: none"{% endif %}>
                {% if not line.fields|length_is:'1' and not field.is_readonly %}{{ field.errors }}{% endif %}
                {% if field.is_checkbox %}
                    {{ field.field }}{{ field.label_tag }}
                {% else %}
                    {# only show the label for visible fields #}
                    {% if not field.field.is_hidden %}
                    {{ field.label_tag }}
                    {% endif %}

                    {% if field.is_readonly %}
                        <p>{{ field.contents }}</p>
                    {% else %}
                        {{ field.field }}
                    {% endif %}
                {% endif %}
                {% if field.field.help_text %}
                    <p class="help">{{ field.field.help_text|safe }}</p>
                {% endif %}
            </div>
        {% endfor %}
    </div>
{% endfor %}

Score: 0

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

In Django forms, the ':' after the 5 labels is the label_suffix. You can change or remove 4 the label_suffix by creating a subclass of ModelForm, here called 3 UnstyledForm, and redefining the initialization function 2 with label_suffix set to an empty string. Then use 1 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!

Score: 0

Another way to do it, but i think it still 1 better to iterate form.visible_fields & form.hidden_fields

<form action="{% url 'some_url' param %}" method="POST">
    {% csrf_token %}
    <div class="row">
        {% for field in form %}

            {% if not field.is_hidden %}
                <div class="col-md-6">
                    {{ field.label_tag }}
                    {{ field.error }}
                    {{ field }}
                </div>
            {% else %}
                {{ field }}
            {% endif %}
        {% endfor %}
     </div>
</form>

More Related questions