0

I have the following code for my login:

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

However when I have debug set to false on my site, whenever the login is incorrect I get a 500 error instead of the form displaying the ValidationError. I am calling it like:

if form.is_valid():
    user = form.Login()

That is in a view method. What am I missing here? How do I properly call this form error so it doesn't 500 when debug is set to false?

4

1 回答 1

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
于 2013-06-26T03:57:43.750 回答