Django forms.ValidationError 被500.html拦截怎么办?
表单代码:
class UserCreationForm(forms.ModelForm):
"""A form for creating new users. Includes all the required
fields, plus a repeated password."""
password1 = forms.CharField(label='Password', widget=forms.PasswordInput, error_messages={'required':'密码不能为空'})
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput, error_messages={'required':'密码不能为空'})
class Meta:
model = MyUser
fields = ('user_name', 'password')
def clean_password1(self):
password1 = self.cleaned_data.get("password1")
pattern = re.compile('^(?=.*?\d)(?=.*?[a-zA-Z]).{8,}$')
if not pattern.match(password1):
raise forms.ValidationError("密码过于简单,请至少8位,并且同时包含字母和数字")
return password1
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("两次输入密码不一致")
return password2
def save(self, commit=True):
# Save the provided password in hashed format
user = super().save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
view.py:
def register(request):
if request.method == 'POST':
myuser_form = MyUserForm(data=request.POST)
if myuser_form.is_valid():
user = myuser_form.save()
user.save()
return HttpResponseRedirect("/accounts/login/")
else:
print(myuser_form.errors)
else:
myuser_form = UserCreationForm()
return render(request,'accounts/register.html',
{'form': myuser_form})
在debug模式下,能够正常运行,将error传递到前端页面,
但是当关掉debug,进入生产模式后,所有的forms.ValidationError全都被500.html拦截了,本来想在注册页面显示error提示,但现在全被500.html当成错误处理,这怎么办?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论