有没有办法更改用户身份验证登录页面中的用户名字段标签?
Is there any way to change username field label in user authentication login page?
Right now login page looked like this
我想将用户名字段的标签更改为团队名称
注意:我使用的是内置的 LoginView
根据 documentation LoginView
有一个名为 authentication_form
的属性(通常只是一种形式 class)。默认为 AuthenticationForm
.
您可以创建一个继承自 AuthenticationForm
的表单 class,设置用户名字段的标签并将其分配给您的 LoginView
over authentication_form
属性。
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'),
]
只需像这样更改用户名字段的标签文本:
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'
您所要做的就是覆盖 Django 管理员身份验证表单。
在我的例子中,我需要覆盖用户名标签,因为我想让用户使用他们的 phone 号码或电子邮件登录。
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
Right now login page looked like this
我想将用户名字段的标签更改为团队名称
注意:我使用的是内置的 LoginView
根据 documentation LoginView
有一个名为 authentication_form
的属性(通常只是一种形式 class)。默认为 AuthenticationForm
.
您可以创建一个继承自 AuthenticationForm
的表单 class,设置用户名字段的标签并将其分配给您的 LoginView
over authentication_form
属性。
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'),
]
只需像这样更改用户名字段的标签文本:
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'
您所要做的就是覆盖 Django 管理员身份验证表单。 在我的例子中,我需要覆盖用户名标签,因为我想让用户使用他们的 phone 号码或电子邮件登录。
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