简体   繁体   中英

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?

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

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

Hope that helps!

关于自定义标签Django 文档说它可以通过表单构造函数的auto_id参数关闭:

f = ContactForm(auto_id=False)

Try this in your form:

def __init__(self, *args, **kwargs):
    self.fields['comment'].label = ''

But for newer versions of django i prefer Iemonad's answer

Not sure about old Django but u can now empty the form field labels in Meta for the new Django

class CustomForm(forms.Form):
    class Meta:
        ...  #other properties such as model, fields, widgets and help text
        labels = {
           'comment' : '',
        }

A quick-and-dirty solution would be to iterate through the form manualy (with {% for field in form %}) and handle the "problematic" field specially. You could also override the as_p/as_table methods if needed.

here is another solution that had worked for me with this

{% for field in form %} {{field.errors}} {% endfor %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM