Remove Labels in a Django Crispy Forms

20,471

Solution 1

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

Solution 2

Just do:

self.helper.form_show_labels = False

To remove all labels.

Solution 3

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>

Solution 4

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.

Solution 5

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)
Share:
20,471
Ron
Author by

Ron

"Nothing is ever easy." — Zedd

Updated on July 09, 2022

Comments

  • Ron
    Ron almost 2 years

    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.

  • CpILL
    CpILL over 7 years
    Hides the label for checkboxes and radio buttons :(
  • Yannic Hamann
    Yannic Hamann about 6 years
    self.fields['MYFIELD'].label = False to disable for a specific field
  • Vadorequest
    Vadorequest over 5 years
    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
  • Admin
    Admin about 5 years
    @YannicHamann 's solution worked for me. I think this needs to be highlighted as a separate answer.
  • Greg Kaleka
    Greg Kaleka almost 5 years
    Note this only works for Bootstrap per the docs.
  • Nitwit
    Nitwit over 3 years
    page not found
  • Adrian
    Adrian over 3 years
    thank you - it works just as described. I added the function in my form and it just worked
  • Aman Gupta
    Aman Gupta almost 3 years
    @Vadorequest See the code for showing lable on specific field when all is False. See my reply