Luc*_*yne 16

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

您可以创建一个继承自 的表单类AuthenticationForm,设置用户名字段的标签并将其分配给您的LoginViewoverauthentication_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})
    )
Run Code Online (Sandbox Code Playgroud)

视图.py

from django.contrib.auth.views import LoginView

from .forms import CustomAuthenticationForm


class CustomLoginView(LoginView):
    authentication_form = CustomAuthenticationForm
Run Code Online (Sandbox Code Playgroud)

网址.py

urlpatterns = [
    path('login/', CustomLoginView.as_view(), name='login'),
]
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记更改 URL - `path('login/', CustomLoginView.as_view(), name='login'),` (2认同)