Search code examples
pythondjangodjango-formsdjango-templatesdjango-widget

How do I get rid of labels in django Form?


I have dealt with only ModelForm previously, so it is my first time using Form. I'd like to get rid of labels from my form, however, how I get rid of labels in ModelForm does not seem to work with Form.

Here is my code:
forms.py

class UserLoginForm(forms.Form):
    email = forms.CharField(max_length=255)
    password = forms.CharField(max_length=255)

    labels = {
        'email': '',
        'password': ''
    }
    widgets = {
        'email': forms.TextInput(attrs={'class': 'login_input', 'placeholder': 'Email'}),
        'password': forms.PasswordInput(attrs={'class': 'login_input', 'placeholder': 'Password'})
    }

It seemed like a simple problem but it turned out I could not get what I wanted from the official django document or Google. I'd be really appreciated if you could help me solving it.


Solution

  • As @Carcigenicate mentioned in the above comment that you can directly use {{form.email}} which would only render input tag instead of label tag.

    To remove the label you should use inline labels not labels dict as they are defined in Meta class, so:

    class UserLoginForm(forms.Form):
        email = forms.CharField(max_length=255, label="")
        password = forms.CharField(max_length=255, label="")
    

    You can also define inline widegts.

    Then you can use {{form}} and won't see labels in the template.