Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Labels in a Django Crispy Forms

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.

like image 246
Ron Avatar asked Jul 13 '12 14:07

Ron


People also ask

How do I remove a form label in Django?

In __init__ method set your field label as empty. This will remove label text. Save this answer.

What is crispy form tags Django?

Django Crispy Forms is a Python package that styles the Django forms with the help of built-in template packs.


3 Answers

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>
like image 30
Lucas B Avatar answered Nov 04 '22 08:11

Lucas B


Just do:

self.helper.form_show_labels = False

To remove all labels.

like image 76
Glyn Jackson Avatar answered Nov 04 '22 08:11

Glyn Jackson


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

like image 8
maraujop Avatar answered Nov 04 '22 10:11

maraujop