Django AuthenticationForm未验证,也没有在数据库中存储信息
因此,基本上,我正在尝试使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定为什么您的
__ Init __
方法无法正常工作,但是您可以在Meta类中添加小部件的定义:让我知道它是否有效:)
I am not sure why your
__init__
method does not work well, but you can add the widget definitions in the Meta class:Let me know if it works :)