Django AuthenticationForm未验证,也没有在数据库中存储信息

发布于 2025-01-25 10:32:42 字数 2498 浏览 0 评论 0原文

因此,基本上,我正在尝试使用Django AuthenticationForm和LoginView进行简单的登录系统。该表单获取用户输入,并且按下登录按钮时,页面只是重新加载,而不是丢弃验证错误或记录用户并重定向他。我试图覆盖干净的方法,但结果是相同的。我不知道我在做什么错。

这是用户模型:

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类定义字段时,一切都可以正常工作,并且用户被成功地登录和重定向,但是通过这种方式,野外小部件保持不变,因为元素在 init 之前被元口解析。我还试图手动将字段添加到自我中。字段:

 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", }))

但是,再次未登录用户,并且数据未验证。我认为我的错误是在现场验证中。 不胜感激的是,请事先感谢!

So basically I'm trying to do a simple login system using Django AuthenticationForm and LoginView. The form takes the user input and when the login button is pressed, the page just reloads, instead of throwing validation errors or logging the user and redirecting him. I've tried to overwrite the clean method but the result is the same. I don't know what I'm doing wrong.

This is the User model:

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()

Here is the Login view:

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

And also the Form:

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

And the 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>

When the fields are defined through the Meta class everything works fine, and the user is logged in and redirected successfully, but in this way, the field widgets remain unchanged because Meta is parsed by the metaclass before the init. I also tried to add the field manually to self. fields:

 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", }))

But again and the user is not logged in and the data is not validated. I think my mistake is in the field validation.
Аny help will be appreciated, thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

真心难拥有 2025-02-01 10:32:42

我不确定为什么您的__ Init __方法无法正常工作,但是您可以在Meta类中添加小部件的定义:

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", })
       }

让我知道它是否有效:)

I am not sure why your __init__ method does not work well, but you can add the widget definitions in the 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", })
       }

Let me know if it works :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文