无法登录Django-Admin但可以使用自定义表单登录

时间:2017-02-28 17:08:38

标签: 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', {})

上述视图在进行身份验证时有效。但是,当通过我的登录页面或/ admin /登录时,我会从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': 'admin@admin.com'}

相关 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,而在我的视图中,我提取了email和{{ 1}}。当然应该正确使用password方法,但根据我的日志,它永远不会被击中。

2 个答案:

答案 0 :(得分: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', {})

答案 1 :(得分: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())