[Django]-Remove Labels in a Django Crispy Forms

8👍

You could edit the field.html template:
https://github.com/maraujop/django-crispy-forms/blob/dev/crispy_forms/templates/bootstrap/field.html#L7

Add a FormHelper attribute to your form that controls the label rendering and use it in that template if. Custom FormHelper attributes are not yet officially documented, because I haven’t had time, but I talked about them in a keynote I gave, here are the slides:
https://speakerdeck.com/u/maraujop/p/django-crispy-forms

49👍

Just do:

self.helper.form_show_labels = False

To remove all labels.

20👍

Works with Boostrap ( see documentation )

In your form :

from crispy_forms.helper import FormHelper
from django import forms

class MyForm(forms.Form):
    [...]
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_show_labels = False 

In your template:

<form method='POST' action=''>{% csrf_token %}
{% crispy form %}
<input type='submit' value='Submit' class='btn btn-default'>
</form>

6👍

The solution below lets you remove a label from both a regular or crispy control. Not only does the label text disappear, but the space used by the label is also removed so you don’t end up with a blank label taking up space and messing up your layout.

The code below works in django 2.1.1.

# this class would go in forms.py
class SectionForm(forms.ModelForm):
    # add a custom field for calculation if desired
    txt01 = forms.CharField(required=False)

    def __init__(self, *args, **kwargs):
        ''' remove any labels here if desired
        '''
        super(SectionForm, self).__init__(*args, **kwargs)

        # remove the label of a non-linked/calculated field (txt01 added at top of form)
        self.fields['txt01'].label = ''

        # you can also remove labels of built-in model properties
        self.fields['name'].label = ''

    class Meta:
        model = Section
        fields = "__all__"

I’m not clear what the problem the OP had with the code snippet he showed, except that he wasn’t putting the line of code in the right place. This seems like the best and simplest solution.

4👍

if you are only to remove some labels from input, then explicitly don’t give a label name in model definition, i.e:

field = models.IntegerField("",null=True)
👤Alist

0👍

To Remove All Labels:

self.helper.form_show_labels = False

To Show Specific Lable when all False :

HTML('<span>Your Label</span>')

To Disable Label for Specific field when all is True

self.fields['fieldName'].label = True

Example:

     Row(
            HTML('<span> Upolad Government ID (Adhar/PAN/Driving Licence)</span>'),
            Column('IdProof',css_class='form-group col-md-12 mb-0'),
            css_class='form-row'
        ),

0👍

This way you can remove label from field, or change default label text.

class ModelNameForm(forms.ModelForm):

    class Meta:
        model = OfferResidential
        fields = ['field_with_label', 'field_no_label']
        
        # all field mentioned here, with value '' will be shown without label
        labels = {
            'field_no_label': '',
        }

Leave a comment