Right now login page looked like this
I want to change label of username field to Team Name
Note: I'm using builtin LoginView
Right now login page looked like this
I want to change label of username field to Team Name
Note: I'm using builtin LoginView
According to documentation LoginView has an attribute called authentication_form (typically just a form class). Defaults to AuthenticationForm.
You can create a form class that inherits from AuthenticationForm, set the label of the username field and assign it to your LoginView over authentication_form attribute.
forms.py
from django import forms
from django.contrib.auth.forms import AuthenticationForm, UsernameField
class CustomAuthenticationForm(AuthenticationForm):
username = UsernameField(
label='Team Name',
widget=forms.TextInput(attrs={'autofocus': True})
)
views.py
from django.contrib.auth.views import LoginView
from .forms import CustomAuthenticationForm
class CustomLoginView(LoginView):
authentication_form = CustomAuthenticationForm
urls.py
urlpatterns = [
path('login/', CustomLoginView.as_view(), name='login'),
]
Just change the label's text of your username field like this:
class LoginForm(ModelForm):
class Meta:
model = YourModel
fields = ['username','password']
def __init__(self, *args, **kwargs):
super(LoginForm, self).__init__(*args, **kwargs)
for fieldname in ['username']:
self.fields[fieldname].label = 'Email'
All you have to do is override the Django admin authentication form. In my case, I needed to override the username label since I wanted to allow users to use either their phone number or email to log in.
forms.py
from django import forms
from django.contrib.admin.forms import AdminAuthenticationForm
from django.contrib.auth.forms import AuthenticationForm, UsernameField
from django.utils.translation import gettext_lazy as _
class CustomAdminAuthenticationForm(AdminAuthenticationForm):
"""
A custom authentication form used in the admin app.
"""
error_messages = {
**AuthenticationForm.error_messages,
'invalid_login': _(
"Please enter the correct phone number or email and password for a staff "
"account. Note that both fields may be case-sensitive."
),
}
username = UsernameField(
label='Email/Phone number',
widget=forms.TextInput(attrs={'autofocus': True})
)
admin.py
import CustomAdminAuthenticationForm from {whatever_location}
from django.contrib import admin
admin.site.login_form = CustomAdminAuthenticationForm