創建UserProfile模型後。我開始創建登錄,但由於get_user()錯誤而卡住了。Django 1.5 - 登錄
EXCEPTION
AttributeError: 'LoginForm' object has no attribute 'get_user'
這裏是我的代碼:
UPDATE
class LoginView(FormView):
form_class = LoginForm
redirect_field_name = REDIRECT_FIELD_NAME
template_name = 'login.html'
success_url = '/'
def form_valid(self, form):
username = form.cleaned_data['username']
password = form.cleaned_data['password']
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(self.request, user)
return HttpResponseRedirect(self.get_success_url())
else:
return self.form_invalid()
def form_invalid(self):
return HttpResponseRedirect(reverse('accounts:login'))
def get_success_url(self):
if self.success_url:
redirect_to = self.success_url
else:
redirect_to = self.request.REQUEST.get(self.redirect_field_name, '')
netloc = urlparse.urlparse(redirect_to)[1]
if not redirect_to:
redirect_to = settings.LOGIN_REDIRECT_URL
elif netloc and netloc != self.request.get_host():
redirect_to = settings.LOGIN_REDIRECT_URL
return redirect_to
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid()
如何解決這一問題?任何幫助,將不勝感激。我在Django 1.5上真的很新。需要幫忙。
我發現這個文檔中:如果已經定製認證(請參閱自定義身份驗證),您可以通過自定義的認證形式向登錄視圖通過authentication_form參數。該表單必須接受其__init__方法中的請求關鍵字參數,並提供一個返回經過身份驗證的用戶對象的get_user方法(此方法僅在成功驗證表單後纔會調用)。 – 2013-03-09 15:04:07
你能發表這個LoginForm的代碼嗎? – 2013-03-09 15:04:52
@AdriánLópez完成 – catherine 2013-03-09 15:07:04