用户属性未附加到mongoengine auth中后续请求中的请求对象

时间:2015-03-02 18:22:49

标签: django mongoengine django-authentication

我已尝试使用以下链接获取登录解决方案,但是当我执行/登录时,它最初会将用户属性添加到请求中。当我之后进行后续调用时,用户属性不会持久存储在请求对象上。

MongoEngine User authentication (django)

在我的settings.py中,我导入了AuthenticationMiddleware,因此它应该保留用户属性,但不是。

这是我的views.py:

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/'

1 个答案:

答案 0 :(得分:0)

你忘了告诉Django使用mongoengine后端。

在您的settings.py文件中添加以下内容:

AUTHENTICATION_BACKENDS = (
    'mongoengine.django.auth.MongoEngineBackend',
)

使用您的代码进行测试,并按预期工作。