大佬教程收集整理的这篇文章主要介绍了Django 错误、登录和注册表单适用于标准方法,但不适用于基于类的视图,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我想请你帮忙。我想用基于类的视图重写我的身份验证。我找到了几个教程,它们都在讲述相同的内容。 urls.py:
# Authorization:
path('signup/',vIEws.SignUpVIEw.as_vIEw(),name='signup'),path('login/',auth_vIEws.LoginVIEw.as_vIEw(),name='login'),path('logout/',auth_vIEws.logoutVIEw,name='logout'),
vIEws.py:
class SignUpVIEw(generic.CreateVIEw):
form_class = UserCreationForm
success_url = reverse_lazy('home')
template_name = 'registration/signup.HTML'
这是我的 signup.HTML 的一部分:
<div class="jumbotron jumbotron-fluID vertical-center">
<div class="container">
<div class="d-flex justify-content-center">
{% if error %}
<div style="text-align: center;">
<div class="alert alert-danger" role="alert">{{ error }}</div>
</div>
{% endif %}
<div class="row w-75">
<div class="col-md-12">
<h1>Sign Up:</h1><br>
<form method="post">
{% csrf_token %}
<div class="form-group">
<label for="exampleinputEmail1">Username:</label>
<input type="text" class="form-control" name="username" aria-describedby="emailHelp"
placeholder="Enter username. It will be visible for others." required>
</div>
<br>
<div class="form-group">
<label for="exampleinputpassword1">password:</label>
<input type="password" class="form-control" name="password1" ID="exampleinputpassword1"
placeholder="password. Please make it strong and unique." required>
</div>
<br>
<div class="form-group">
<label for="exampleinputpassword1">Confirm password:</label>
<input type="password" class="form-control" name="password2" ID="exampleinputpassword1"
placeholder="Repeat password from above." required>
</div>
<br>
<button type="submit" class="btn btn-lg btn-success">Sign Up</button>
</form>
</div>
</div>
</div>
</div>
</div>
而且它不起作用。我可以在控制台中看到 POST 200
已发送,但没有重定向到主页。当我尝试登录时,什么也没有发生。也没有出现错误(密码不匹配等),我找不到任何教程或文章来展示向用户显示错误的实现。
我不明白我哪里出错了。因为在教程中(除了不向用户显示任何错误),他们至少有工作重定向和成功的用户创建。
我哪里做错了?
编辑:
forms.py 中的 UserCreationForm: (我相信我这里没有做任何改动)
class UserCreationForm(forms.ModelForm):
"""
A form that creates a user,with no privileges,from the given username and
password.
"""
error_messages = {
'password_mismatch': _('The two password fIElds dIDn’t match.'),}
password1 = forms.CharFIEld(
label=_("password"),Strip=false,Widget=forms.passwordinput(attrs={'autocomplete': 'new-password'}),Help_text=password_valIDation.password_valIDators_Help_text_HTML(),)
password2 = forms.CharFIEld(
label=_("password confirmation"),Help_text=_("Enter the same password as before,for verification."),)
class Meta:
model = User
fIElds = ("username",)
fIEld_classes = {'username': UsernameFIElD}
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
if self._Meta.model.USERname_FIELD in self.fIElds:
self.fIElds[self._Meta.model.USERname_FIELD].Widget.attrs['autofocus'] = True
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise ValIDationError(
self.error_messages['password_mismatch'],code='password_mismatch',)
return password2
def _post_clean(self):
super()._post_clean()
# ValIDate the password after self.instance is updated with form data
# by super().
password = self.cleaned_data.get('password2')
if password:
try:
password_valIDation.valIDate_password(password,self.instancE)
except ValIDationError as error:
self.add_error('password2',error)
def save(self,commit=TruE):
user = super().save(commit=falsE)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
您似乎没有在视图中处理表单?
class SignUpView(generic.CreateView):
form_class = UserCreationForm
success_url = reverse_lazy('home')
template_name = 'registration/signup.html'
没有任何表单处理方法。
试试:
class SignUpView(generic.CreateView):
form_class = UserCreationForm
success_url = reverse_lazy('home')
template_name = 'registration/signup.html'
def post(self,request,*args,**kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
form.save() //handle user creation here.
return self.form_valid(form)
以上是大佬教程为你收集整理的Django 错误、登录和注册表单适用于标准方法,但不适用于基于类的视图全部内容,希望文章能够帮你解决Django 错误、登录和注册表单适用于标准方法,但不适用于基于类的视图所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。