Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing the Label From Django's TextArea Widget

How do I remove the label that comes attached to the TextArea I am trying to use with Django? I'm trying to find ANY information about this issue but I cannot seem to find anything relating to my problem. This is what I'm doing in my code:

class CommentForm(forms.Form):
    comment = forms.CharField(widget=forms.Textarea())

This is the HTML that it produces:

<label for="id_text">Text:</label> 
<textarea id="id_text" rows="10" cols="40" name="text"></textarea>

That label is no good and I'd like a way to remove it. That code was produced via:

{{ form.as_p }}

(I removed the paragraph tags because they are irrelevant)

EDIT: I added the class CommentForm part for further clarification.

Anyone have any suggestions?

like image 472
AlbertoPL Avatar asked Jul 03 '09 19:07

AlbertoPL


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.

What is label in Django?

label is used to change the display name of the field. label accepts as input a string which is new name of field. The default label for a Field is generated from the field name by converting all underscores to spaces and upper-casing the first letter.

What does Widget do in Django?

A widget is Django's representation of an HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. The HTML generated by the built-in widgets uses HTML5 syntax, targeting <!


2 Answers

This should work with the latest version (trunk) of django:

comment = forms.CharField(label="", help_text="", widget=forms.Textarea())

Hope that helps!

like image 59
lemonad Avatar answered Sep 19 '22 08:09

lemonad


The Django documentation on customizing labels says it could be turned off with auto_id argument to Form constructor:

f = ContactForm(auto_id=False)
like image 23
Grzegorz Oledzki Avatar answered Sep 22 '22 08:09

Grzegorz Oledzki