Django AuthenticationForm 不验证也不在数据库中存储信息

Django AuthenticationForm is not validating and not storing information in database

所以基本上我正在尝试使用 Django AuthenticationForm 和 LoginView 做一个简单的登录系统。该表单接受用户输入,当按下登录按钮时,页面会重新加载,而不是抛出验证错误或记录用户并重定向他。我试图覆盖 clean 方法,但结果是一样的。我不知道我做错了什么。

这是用户模型:

class StoreUser(auth_models.AbstractBaseUser, auth_models.PermissionsMixin):
  email = models.EmailField(
    unique=True,
)
  date_joined = models.DateTimeField(
    auto_now_add=True,
)

  is_staff = models.BooleanField(
    default=False,
)

  USERNAME_FIELD = 'email'

  objects = StoreUserManager()

这是登录视图:

class UserLoginView(auth_views.LoginView):
   form_class = UserLoginForm
   template_name = 'store_auth/user_login.html'
   success_url = reverse_lazy('homepage')

还有表格:

class UserLoginForm(auth_forms.AuthenticationForm):
   email = forms.EmailField(widget=forms.EmailInput(attrs={'class': 'form-control', 'id': "email-login", 'placeholder': "Enter your email",}))
   password = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control', 'id':"password-login", 'labels': "Password", 'placeholder': "Enter your password", }))

   class Meta:
    model = StoreUser

和 HTML:

  <form method="post" action="{% url 'user login' %}">
              {% csrf_token %}
            <div class="form-group">
            <label for="email-login">E-Mail</label>
              {{ form.email }}
            </div>
            <div class="form-group">
            <label for="password-login">Password</label>
              {{ form.password }}
            </div>
            <a href="javascript:;">Forgotten Password?</a>
            <div class="padding-top-20">                  
              <button class="btn btn-primary" type="submit">Login</button>
            </div>

当通过 Meta class 定义字段时,一切正常,用户登录并成功重定向,但在这种情况下,字段小部件保持不变,因为 Meta 由 metaclass 在 init 之前。我还尝试手动将字段添加到 self.字段:

 def __init__(self, *args, **kwargs):
   super(UserLoginForm, self).__init__(*args, **kwargs)
   self.fields['email'] = forms.EmailField(widget=forms.EmailInput(attrs={'class': 'form-control', 'id': "email-login", 'placeholder': "Enter your email",}))
   self.fields['password'] = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'form-control', 'id':"password-login", 'labels': "Password", 'placeholder': "Enter your password", }))

但又一次,用户未登录且数据未验证。我认为我的错误在于现场验证。 感谢您的帮助,提前致谢!

我不确定为什么你的 __init__ 方法不能正常工作,但你可以在 Meta class:

中添加小部件定义
class UserLoginForm(auth_forms.AuthenticationForm):
   email = forms.EmailField()
   password = forms.CharField()

   class Meta:
       # AuthenticationForm is not a ModelForm so the line below is
       # invalid. Your User model will be retrieved through the 
       # AUTH_USER_MODEL setting
       # model = StoreUser  
       fields = ['email', 'password']
       widgets = {
           'email': forms.EmailInput(attrs={'class': 'form-control', 'id': "email-login", 'placeholder': "Enter your email",}),
           'password': forms.PasswordInput(attrs={'class': 'form-control', 'id':"password-login", 'labels': "Password", 'placeholder': "Enter your password", })
       }

让我知道它是否有效:)