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?
In __init__ method set your field label as empty. This will remove label text.
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.
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 <!
This should work with the latest version (trunk) of django:
comment = forms.CharField(label="", help_text="", widget=forms.Textarea())
Hope that helps!
The Django documentation on customizing labels says it could be turned off with auto_id
argument to Form constructor:
f = ContactForm(auto_id=False)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With