Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to change username field label in user authentication login page?

Right now login page looked like this

I want to change label of username field to Team Name

Note: I'm using builtin LoginView

like image 873
user7387275 Avatar asked Dec 30 '18 19:12

user7387275


2 Answers

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'),
]
like image 158
Lucas Weyne Avatar answered Nov 19 '22 21:11

Lucas Weyne


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'
like image 1
Ash Singh Avatar answered Nov 19 '22 22:11

Ash Singh