gpt4 book ai didi

django - 有什么方法可以更改用户身份验证登录页面中的用户名字段标签?

转载 作者:行者123 更新时间:2023-12-03 09:59:07 25 4
gpt4 key购买 nike

Right now login page looked like this

我想将用户名字段的标签更改为团队名称

注意:我正在使用内置的 LoginView

最佳答案

根据 documentation LoginView有一个名为 authentication_form 的属性(通常只是一个表单类)。默认为 AuthenticationForm .

您可以创建一个继承自 AuthenticationForm 的表单类。 ,设置用户名字段的标签,并将其分配给您的LoginViewauthentication_form属性。

表格.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})
)

View .py
from django.contrib.auth.views import LoginView

from .forms import CustomAuthenticationForm


class CustomLoginView(LoginView):
authentication_form = CustomAuthenticationForm

网址.py
urlpatterns = [
path('login/', CustomLoginView.as_view(), name='login'),
]

关于django - 有什么方法可以更改用户身份验证登录页面中的用户名字段标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53980603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com