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

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

    Another way to do it, but i think it still 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>
    
    0 讨论(0)
  • 2020-12-31 03:23

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

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

    Currently, the best solution is to override the admin fieldset template. Use a HiddenInput for your widget, then override the admin fieldset template (documented here). Just create 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>
    
    0 讨论(0)
  • 2020-12-31 03:27

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

    0 讨论(0)
  • 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:

        <form>
            <span>{{ field.label_tag }} {{ field }}</span>
        </form>
    

    Your context must include:

    'form_field_container': 'span'
    

    You get the idea...

    0 讨论(0)
  • 2020-12-31 03:31

    I think it's simpler to achieve the ":" label omission for HiddenInput widget by modifying 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
    
    0 讨论(0)
  • 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?)

    0 讨论(0)
提交回复
热议问题