Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法验证超级用户身份或登录Django中的管理站点,我做错了什么?_Python_Django_Django Admin_Django Authentication - Fatal编程技术网

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

Python 无法验证超级用户身份或登录Django中的管理站点,我做错了什么?,python,django,django-admin,django-authentication,Python,Django,Django Admin,Django Authentication,我正在使用django rest构建一个restful API。我还想把它与内置的django管理功能。我使用/manage.py createsuperuser命令创建了一个超级用户。当我尝试使用正确的凭据登录创建的用户时,我无法登录。 在研究中,我发现我的设置中没有身份验证\u后端。我添加了身份验证后端,但这不起作用 my settings.py文件: import os # Build paths inside the project like this: os.path.join(BA

我正在使用django rest构建一个restful API。我还想把它与内置的django管理功能。我使用
/manage.py createsuperuser
命令创建了一个超级用户。当我尝试使用正确的凭据登录创建的用户时,我无法登录。 在研究中,我发现我的设置中没有
身份验证\u后端。我添加了身份验证后端,但这不起作用

my 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
当我尝试在shell中进行身份验证时,它返回None

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

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

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

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

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

我终于找到了解决办法,在设置中将
中间件
更改为
中间件_类
,效果非常好


我真的不需要明确地指定
身份验证\u后端。

我终于找到了解决办法,在设置中将
中间件
更改为
中间件\u类
,效果非常好


我真的不需要明确地指定
身份验证\u后端

我认为这会很有效,我会回来写它,但不幸的是,这并不能解决问题。我想知道rest\u框架身份验证是否会造成干扰,但似乎不可能。我确实有一个与您类似的设置,我使用django admin后端和rest API。并比较了设置文件。您和我之间的差异,即我没有显式的身份验证\u后端和rest框架默认的\u身份验证\u类。你能看到本地设置是否覆盖了任何内容吗?它将调试模式覆盖为
True
和db设置。我原以为这会很有效,我会回来写的,但不幸的是,这并不能解决问题。我想知道rest\u框架身份验证是否会造成干扰,但似乎不可能。我确实有一个与您类似的设置,我使用django admin后端和rest API。并比较了设置文件。您和我之间的差异,即我没有显式的身份验证\u后端和rest框架默认的\u身份验证\u类。您能看到本地_设置是否覆盖了任何内容吗?它将调试模式覆盖为
True
和db设置。