Django 在mongoengine auth的后续请求中,用户属性未附加到请求对象
我曾尝试使用下面的链接作为登录解决方案,但当我使用/login时,它最初会将user属性添加到请求中。在之后进行后续调用时,用户属性不会持久化到请求对象上 在my settings.py中,我确实导入了AuthenticationMiddleware,因此它应该保留用户属性,但不保留其属性 以下是我的视图.py:Django 在mongoengine auth的后续请求中,用户属性未附加到请求对象,django,mongoengine,django-authentication,Django,Mongoengine,Django Authentication,我曾尝试使用下面的链接作为登录解决方案,但当我使用/login时,它最初会将user属性添加到请求中。在之后进行后续调用时,用户属性不会持久化到请求对象上 在my settings.py中,我确实导入了AuthenticationMiddleware,因此它应该保留用户属性,但不保留其属性 以下是我的视图.py: from django.contrib.auth import login, logout, authenticate from mongoengine.django.auth im
from django.contrib.auth import login, logout, authenticate
from mongoengine.django.auth import User
from mongoengine.queryset import DoesNotExist
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
import json
def login_view(request):
user = User.objects.get(username='john')
user.backend = 'mongoengine.django.auth.MongoEngineBackend'
login(request, user)
request.session.set_expiry(60 * 60 * 1) # 1 hour timeout
if(request.user.is_authenticated()):
return HttpResponse(request.user.username)
else:
return HttpResponse('Not Authenticated!')
def do_something(request):
return HttpResponse(request.user.username)
这是我的settings.py:
"""
Django settings for questiveAuth project.
For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '%skvtelk+53alr^y8lr4z5d1+4_!_kgmy8%w0ip_%e&^ug$kas'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
LOGIN_URL = '/loginredirect/'
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.auth',
'mongoengine.django.mongo_auth',
'rest_framework'
)
AUTH_USER_MODEL = 'mongo_auth.MongoUser'
MONGOENGINE_USER_DOCUMENT = 'mongoengine.django.auth.User'
SESSION_ENGINE = 'mongoengine.django.sessions'
SESSION_SERIALIZER = 'mongoengine.django.sessions.BSONSerializer'
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
)
ROOT_URLCONF = 'questiveAuth.urls'
WSGI_APPLICATION = 'questiveAuth.wsgi.application'
from mongoengine import *
connect('questAuth')
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.dummy'
}
}
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
)
# Internationalization
# https://docs.djangoproject.com/en/1.7/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.7/howto/static-files/
STATIC_URL = '/static/'
你忘了告诉Django改用mongoengine后端 在settings.py文件中添加以下内容:
AUTHENTICATION_BACKENDS = (
'mongoengine.django.auth.MongoEngineBackend',
)
使用您的代码进行测试,并按预期工作