2014-01-24 93 views
1

任何人都可以提供更多關於自定義Django認證系統的信息。好吧,我創建了我使用電子郵件作爲標識符的自定義用戶:自定義用戶認證

USERNAME_FIELD = 'email' 

和我創建了一個自定義的認證後端,和我換了認證後端到這個新設置文件:

class Custom_Authentication(ModelBackend): 
     def authenticate(self, email=None, password=None): 
      try: 
       user = self.user_class.objects.get(email=email) 
       if user.check_password(password): 
        return user 
      except self.user_class.DoesNotExist: 
        return None 

     def get_user(self, user_id): 
      try: 
       return self.user_class.objects.get(pk=user_id) 
      except self.user_class.DoesNotExist: 
       return None 

     @property 
     def user_class(self): 
      if not hasattr(self, '_user_class'): 
       self._user_class = get_model(*settings.AUTH_USER_MODEL.split('.', 2)) 
       if not self._user_class: 
        raise ImproperlyConfigured('Could not get custom user model') 
      return self._user_class 

但現在我不知道如何使用他的電子郵件驗證自定義的用戶,我用:

from django.contrib import auth 
    def auth_view(request): 
     email = request.POST.get('email', '') 
     password = request.POST.get('password', '') 
     user = auth.authenticate(username=email, password=password) 

     if user is not None: 
      auth.login(request, user) 
      return HttpResponseRedirect('/loggedin/') 
     else: 
      return HttpResponseRedirect('/invalid/') 

我嘗試這樣做,它似乎不承認我創建USI用戶管理界面。還有什麼我必須改變的嗎?

回答