在最近的代碼更改中,我無法再通過/ 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.User
有USERNAME_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
我一直無法牽制導致此問題的確切的代碼改變。唯一的區別是我在默認登錄頁面和管理頁面中傳遞username
和password
,而在我看來,我正在獲取email
和password
。當然def authenticate(self, username, password):
方法應該正確使用,但根據我的日誌,它永遠不會被擊中。
再次保存我的屁股。我會盡可能接受。感謝您的額外信息 –