Django AuthenticationForm无法工作,给予错误名为“UsernameField”未定义

mzmfm0qo  于 2023-07-01  发布在  Go
关注(0)|答案(2)|浏览(101)

这是我的密码

class LoginForm(AuthenticationForm):
    username = UsernameField(label='username', widget=forms.TextInput(attrs={'autofocus':True,'class':'form-control'}))
    password = forms.CharField(label =_("Password"), strip=False, widget=forms.PasswordInput(attrs={'autocomplete':'current-password', 'class':'form-control'}))

当我运行服务器时,发生此错误(NameError:未定义名称“UsernameField”)

class LoginForm(AuthenticationForm):
  File "E:\07_Django_All\03_ecommerce_site(Project)\Ecommerce_site\app\forms.py", line 19, in LoginForm
    username = UsernameField(label='username', widget=forms.TextInput(attrs={'autofocus':True,'class':'form-control'}))
NameError: name 'UsernameField' is not defined
bfnvny8b

bfnvny8b1#

必须从django.contrib.auth.forms导入UsernameField

from django.contrib.auth.forms import AuthenticationForm, UsernameField
b4lqfgs4

b4lqfgs42#

1.必须从django.contrib.auth.forms导入UsernameField

from django.contrib.auth.forms import AuthenticationForm, UsernameField

1.必须从django.utils.translation导入gettextgettext_lazy as _

from django.utils.translation import gettext,gettext_lazy as _

相关问题