简体   繁体   English

无法验证超级用户或登录Django的管理站点,我做错了什么?

[英]Not able to authenticate superuser or login into admin site in Django, what am I doing wrong?

Am building a restful API using django rest. 我正在使用django rest构建一个安静的API。 I also want took it with the inbuilt django admin functionalities. 我也想用内置的django管理功能。 I created a super user using the ./manage.py createsuperuser command. 我使用./manage.py createsuperuser命令创建了一个超级用户。 When I tried to login the created user using the correct credentials I could not. 当我尝试使用正确的凭据登录创建的用户时,我无法。 On researching I discovered I did not have AUTHENTICATION_BACKEND in my settings. 在研究中,我发现我的设置中没有AUTHENTICATION_BACKEND I added authentication backends and that did not work. 我添加了身份验证后端,但是没有用。

my settings.py file: 我的settings.py文件:

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '1_ebkw5nvkp^=+eaf7njk&wrlmcqj(u)(+#+wf#5kko@4va%am'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.postgres',
    'rest_framework',
    'rest_framework.authtoken',

    'api_v1',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'bucketlists.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'bucketlists.wsgi.application'

AUTHENTICATION_BACKENDS = ('django.contrib.auth.backends.ModelBackend',)


# Database
# https://docs.djangoproject.com/en/1.10/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'buckos',
        'USER': 'postgres',
        'TEST': {'CHARSET': 'UTF8'}
    }
}

# Password validation
# https://docs.djangoproject.com/en/1.10/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.TokenAuthentication',),
}


# Internationalization
# https://docs.djangoproject.com/en/1.10/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

STATIC_URL = '/static/'

# override production settings during development
try:
    from .local_settings import *
except ImportError as e:
    pass

When I try to authenticate in shell it returns None. 当我尝试在shell中进行身份验证时,它返回None。

>>> from django.contrib.auth import authenticate
>>> my = authenticate(username='admin',password='password123')
>>> print(my)
None

您需要将SessionAuthenticationMiddleware添加到您的中间件。

'django.contrib.auth.middleware.SessionAuthenticationMiddleware',

I finally got work around, changed MIDDLEWARE to MIDDLEWARE_CLASSES in settings and it worked like a charm. 我终于解决了,在设置中将MIDDLEWARE更改为MIDDLEWARE_CLASSES ,它就像一个魅力。

And I really didn't need to specify AUTHENTICATION_BACKENDS exlicitly. 而且我真的不需要明确指定AUTHENTICATION_BACKENDS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM