如何在引发表单错误时调试false时,如何将Django表单方法设置为不500?

时间:2013-06-26 03:29:55

标签: django forms

我的登录代码如下:

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?

1 个答案:

答案 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