使用django rest构建一个restful API。我也想用内置的django管理功能。我使用./manage.py createsuperuser
命令创建了一个超级用户。当我尝试使用正确的凭据登录创建的用户时,我无法。
在研究中,我发现我的设置中没有AUTHENTICATION_BACKEND
。我添加了身份验证后端,但是没有用。
我的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
答案 0 :(得分:0)
您需要将SessionAuthenticationMiddleware添加到您的中间件。
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
答案 1 :(得分:0)
我终于解决了问题,在设置中将MIDDLEWARE
更改为MIDDLEWARE_CLASSES
,它就像一个魅力。
而且我真的不需要明确指定AUTHENTICATION_BACKENDS
。