2017-02-28 90 views
1

在最近的代碼更改中,我無法再通過/ admin /或我的默認登錄頁面登錄,但我可以使用頁面登錄重定向到登錄頁面。有問題的重定向:無法登錄到Django-Admin,但可以使用自定義表單登錄

def show_calendar(request): 
    user = None 
    if request.method == "POST": 
     email = request.POST.get('email') 
     password = request.POST.get('password') 
     user = authenticate(email=email, password=password) 
     if user is not None: 
      login(request, user) 
     else: 
      return render(request, 'registration/login.html', {'error': 'Email and password not recognised. Please try again.'}) 
    if request.user.is_authenticated or user: 
     return render(request, 'calendar/full_calendar.html', {}) 
    return render(request, 'registration/login.html', {}) 

上述視圖在進行身份驗證時起作用。然而,通過我的登錄頁面或/管理員登錄時/我從django.contrib.auth.authenticate中得到一些日誌,它似乎總是打This backend doesn't accept these credentials as arguments. Try the next one.

def authenticate(**credentials): 
    import logging 
    logging.basicConfig(filename='example.log', level=logging.DEBUG) 
    """ 
    If the given credentials are valid, return a User object. 
    """ 
    for backend, backend_path in _get_backends(return_tuples=True): 
     logging.debug(credentials) 
     try: 
      inspect.getcallargs(backend.authenticate, **credentials) 
     except TypeError: 
      logging.debug("This backend doesn't accept these credentials as arguments. Try the next one.") 
      # This backend doesn't accept these credentials as arguments. Try the next one. 
      continue 

credentials日誌爲:DEBUG:root:{'password': 'pass', 'username': '[email protected]'}

相關settings.py

INSTALLED_APPS = [ 
    'myapp', 
    'dal', 
    'dal_select2', 
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'blog', 
    'fullcalendar', 
    'crispy_forms', 
    'autoslug', 
    'storages', 
    'boto', 
    'django.contrib.sites', 
    'updown', 
    'stream_django', 
    'postman', 
] 

AUTHENTICATION_BACKENDS = ['myapp.CustomBackend.CustomBackend',] 

AUTH_USER_MODEL = 'myapp.User' 

myapp.UserUSERNAME_FIELD設置爲email

CustomBackend.py

from django.contrib.auth.hashers import check_password 

from myapp.models import User 
from myapp import settings 
import logging 
logging.basicConfig(filename='example.log',level=logging.DEBUG) 


class CustomBackend(object): 

    def authenticate(self, username, password): 
     user = None 
     logging.debug("CUSTOM BACKEND AUTHENTICATE AUTHENTICATE") 
     try: 
      valid_user = User.objects.get(email=username) 
      if valid_user.check_password(password): 
       user = valid_user 
     except User.DoesNotExist: 
      pass 
     return user 


    def authenticate(self, email, password): 
     user = None 
     logging.debug("AUTHENTICATE") 
     try: 
      valid_user = User.objects.get(email=email) 
      if valid_user.check_password(password): 
       user = valid_user 
     except User.DoesNotExist: 
      pass 
     return user 

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

我一直無法牽制導致此問題的確切的代碼改變。唯一的區別是我在默認登錄頁面和管理頁面中傳遞usernamepassword,而在我看來,我正在獲取emailpassword。當然def authenticate(self, username, password):方法應該正確使用,但根據我的日誌,它永遠不會被擊中。

回答

0

看起來好像您期望根據參數選擇正確的authenticate方法。但是,在Python中,您的第二個authenticate(self, email, password)方法將替代第一個authenticate(self, username, password):

我不清楚爲什麼你創建了自定義身份驗證後端。既然你在你的自定義用戶模型有USERNAME_FIELD = 'email',默認的模型後端應工作正常使用默認的登錄頁面。

在您的自定義視圖中,將代碼更改爲authenticate(username=email, password=password)。但是,我會避免編寫視圖來儘可能地記錄用戶,以避免錯誤或可能的安全漏洞。

而不是在show_calendar視圖處理登錄,我會使用login_required裝飾器。匿名用戶將被重定向到登錄頁面,然後重定向回視圖一旦他們已經登錄。

from django.contrib.auth.decorators import login_required 

@login_required 
def show_calendar(request): 
    return render(request, 'calendar/full_calendar.html', {}) 
+0

再次保存我的屁股。我會盡可能接受。感謝您的額外信息 –

0

下你pythonanywhere登錄Web選項卡,只檢查WSGI配置文件。 供參考的樣本如下所示:

import os 
import sys 
path='/home/invinciblycool/my-first-blog/' 
if path not in sys.path: 
    sys.path.append(path) 
os.environ['DJANGO_SETTINGS_MODULE']= 'mysite.settings' 
from django.core.wsgi import get_wsgi_application 
from django.contrib.staticfiles.handlers import StaticFilesHandler 
application = StaticFilesHandler(get_wsgi_application()) 
相關問題