20

Does anybody know if there is a correct way to remove labels in a crispy form?

I got as far as this:

self.fields['field'].label = ""

But it's not a very nice solution.

Pablo Claus
  • 5,886
  • 3
  • 29
  • 38
Ron
  • 22,128
  • 31
  • 108
  • 206

6 Answers6

49

Just do:

self.helper.form_show_labels = False

To remove all labels.

Glyn Jackson
  • 8,228
  • 4
  • 29
  • 52
  • 2
    Hides the label for checkboxes and radio buttons :( – CpILL Oct 14 '16 at 14:44
  • 14
    ``self.fields['MYFIELD'].label = False`` to disable for a specific field – Yannic Hamann Apr 20 '18 at 12:03
  • 3
    I tried both `form_show_labels = False` and override a specific field with `self.fields['validation_CGU'].label = True` and it didn't work, seems like the global rule takes priority, too bad – Vadorequest Oct 09 '18 at 21:32
  • @YannicHamann 's solution worked for me. I think this needs to be highlighted as a separate answer. –  Mar 04 '19 at 16:33
  • Note this only works for Bootstrap [per the docs](https://django-crispy-forms.readthedocs.io/en/latest/form_helper.html?highlight=form_show_labels#bootstrap-3-helper-attributes). – Greg Kaleka Jul 26 '19 at 17:35
  • @Vadorequest See the code for showing lable on specific field when all is False. See my reply – Aman Gupta Jun 11 '21 at 12:43
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>
Lucas B
  • 2,183
  • 1
  • 22
  • 22
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

maraujop
  • 4,472
  • 2
  • 36
  • 40
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.

MangoLassi
  • 577
  • 6
  • 7
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
  • 169
  • 2
  • 3
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'
        ),
Aman Gupta
  • 658
  • 1
  • 8
  • 14
  • is not a label, and won't work if you change anything centrally in your css. Don't do that. If you need to show **some** labels and others not, just use tha `.label` way for each. – nerdoc Jul 03 '23 at 09:48