2013-06-26 40 views
0

我有以下代碼爲我登錄:如何將Django表單方法設置爲不500時調試錯誤提高表單錯誤?

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() 

這是在視圖方法。我在這裏錯過了什麼?如果調試設置爲false,我該如何正確調用這個表單錯誤,使其不到500?

+1

應該'Login'不能稱爲'高清clean'? – karthikr

+0

[Django。錯誤消息登錄窗體](http://stackoverflow.com/questions/15084597/django-error-message-for-login-form) – Darren

+0

嗨,你是對的,我的解決方案是在http://stackoverflow.com/問題/ 15084597/django的錯誤消息換登錄狀 – Darren

回答

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