我的登录代码如下:
class LoginForm(forms.Form):
email = forms.EmailField(max_length = 254, min_length = 6)
password = forms.CharField(min_length = 8, widget = forms.PasswordInput())
def Login(self):
email = self.cleaned_data.get('email')
password = self.cleaned_data.get('password')
user = authenticate(email = email, password = password)
if (user is None) or (user is not None and user.is_active is False):
raise forms.ValidationError('Login is incorrect.')
return user
但是当我在我的网站上将调试设置为false时,只要登录不正确,我就会收到500错误而不是显示ValidationError的表单。我称之为:
if form.is_valid():
user = form.Login()
这是一种视图方法。我在这里错过了什么?如何正确调用此表单错误,因此当debug设置为false时它不会500?
答案 0 :(得分:1)
像这样打电话
def clean(self):
password = self.cleaned_data.get('password')
email = self.cleaned_data.get('email')
user = authenticate(email = email, password = password)
if (user is None) or (user is not None and user.is_active is False):
raise forms.ValidationError('Login is incorrect.')
return self.cleaned_data
def login(self,request):
user = authenticate(email = self.cleaned_data.get('email'),password = self.cleaned_data.get('password'))
user = authenticate(username=username, password=password)
return user