In einer letzten Codeänderung kann ich mich nicht mehr über/admin/oder meine Standard-Login-Seite einloggen, aber ich kann mich über eine Seite einloggen das leitet auf die Anmeldeseite um. Die Umleitung in Frage:Ich kann mich nicht bei Django-Admin anmelden, kann mich aber mit benutzerdefiniertem Formular anmelden
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', {})
Die obige Ansicht funktioniert bei der Authentifizierung so. Wenn jedoch über die Anmeldungsseite oder/admin/ich einige Protokolle aus django.contrib.auth.authenticate
anmelden und es scheint immer This backend doesn't accept these credentials as arguments. Try the next one.
zu sein schlagen:
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
Protokolle wie: DEBUG:root:{'password': 'pass', 'username': '[email protected]'}
Relevante 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
hat seine USERNAME_FIELD
Set 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
Ich habe nicht in der Lage gewesen, den genauen Code Änderung festzunageln, die dieses Problem verursacht. Der einzige Unterschied ist, dass ich username
und password
in der Standard-Login-Seite und der Admin-Seite übergebe, während ich aus meiner Sicht die email
und password
holen. Sicherlich sollte die def authenticate(self, username, password):
Methode korrekt verwendet werden, aber gemäß meinen Protokollen wird sie nie getroffen.
Mein Arsch noch einmal gerettet. Wird akzeptieren, wenn ich kann. Danke für die zusätzlichen Informationen –