0

我試圖使用與Django的nonrel用戶授權的自定義後端(電子郵件adrresses而不是用戶名,沒有什麼花哨),它只是不工作。所有致電authenticate()的電話均返回無。這裏是我的後端:我可以使用django-nonrel和自定義auth後端嗎?

from django.contrib.auth.models import User, check_password 

    class EmailAuthBackend(object): 
     """ 
     Email Authentication Backend 

     Allows a user to sign in using an email/password pair rather than 
     a username/password pair. 
     """ 
     supports_anonymous_user = False 
     supports_object_permissions = True 


     def authenticate(self, email=None, password=None): 
      """ Authenticate a user based on email address as the user name. """ 
      try: 
       user = User.objects.get(email=email) 
       if user.check_password(password): 
        return user 
      except User.DoesNotExist: 
       return None 

     def get_user(self, user_id): 
      """ Get a User object from the user_id. """ 
      try: 
       return User.objects.get(pk=user_id) 
      except User.DoesNotExist: 
       return None 

和視圖(我試圖在註冊後立即登錄用戶)。 new_user總是無:

def create_new_user(request): 
    if request.method == 'POST': 
     data = request.POST.copy() 
     data['username'] = ''.join([choice(letters) for i in xrange(30)]) 
     data['password'] = data['password1'] 
     form = CustomUserCreationForm(data) 
     if form.is_valid(): 
      user = form.save(commit=False) 
      # user must be active for login to work 
      user.is_active = True 
      user.save() #not sure if this is actually saving 
      new_user = authenticate(username=data['username'],\ 
        password=data['password']) 
      if new_user is None: 
       messages.error(request,"God damn it.") 
      login(request, new_user) 

      messages.info(request, "Thanks for registering! You are\ 
                  now logged in.") 
      return render_to_response('invoicer/welcome.html', 
                {'user':new_user}) 

     else: 
      messages.error(request, form._errors) 
      return HttpResponseRedirect('//') 

誰能給我一些啓發,爲什麼我到authenticate()調用不工作?


編輯:另外,在部署時,我的註冊表格肯定是創建用戶可在數據查看器中看到appengine.google.com

回答

1

打開了所有我需要做的是改變

的authenticate(用戶名=電子郵件

在視圖中:

身份驗證(電子郵件=電子郵件

我的後端沒有被髮送的電子郵件,以便它默認爲無。對我感到羞恥。然而,由於我使用的是標準的Django的登錄表單,它會通過一個名爲usernamepassword我的後臺,所以我必須要能夠同時處理一個用戶名(實際上是用戶的電子郵件地址)和電子郵件作爲命名的關鍵字。

現在,我只是在做它像這樣在我的後端:

def authenticate(self, username=None, email=None, password=None): 
     """ Authenticate a user based on email address as the user name. """ 
     if username: 
      might_be_email = username 
     elif email: 
      might_be_email = email 

的嘗試might_be_email找到用戶。

相關問題