连接django与mongodb时出错:ImportError:没有名为django.auth的模块

时间:2017-04-05 22:36:54

标签: django mongodb django-models django-templates django-views

我正在尝试使用aws ec2实例中的django和mongodb创建博客

在models.py文件中,我正在进行以下更改

from datetime import datetime
from mongoengine import *
from mongoengine.django.auth import User
from django.core.urlresolvers import reverse

class Post(Document):
    user = ReferenceField(User, reverse_delete_rule=CASCADE)
    title = StringField(max_length=200, required=True)
    text = StringField(required=True)
    text_length = IntField()
    date_modified = DateTimeField(default=datetime.now)
    is_published = BooleanField()

from django.core.urlresolvers import reverse

class Post(Document):
    user = ReferenceField(User, reverse_delete_rule=CASCADE)
    title = StringField(max_length=200, required=True)
    text = StringField(required=True)
    text_length = IntField()
    date_modified = DateTimeField(default=datetime.now)
    is_published = BooleanField()

    def __unicode__(self):
        return self.title

    def save(self, *args, **kwargs):
        self.text_length = len(self.text)
        return super(Post, self).save(*args, **kwargs)

    def get_absolute_url(self):
        return reverse('detail', args=[self.id])

    def get_edit_url(self):
        return reverse('update', args=[self.id])

    def get_delete_url(self):
        return reverse('delete', args=[self.id])

并且,我收到此错误

  Traceback (most recent call last):
      File "models.py", line 7, in <module>
        from mongoengine.django.auth import User 
ImportError: No module named django.auth

并且,在settings.py文件中,我正在进行以下更改

import os
from mongoengine import *
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['54.149.63.203', 'localhost', '127.0.0.1']


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
'LJblog',
'django_extensions',
'mongoengine.django.mongo_auth',

)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'LJ.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 = 'LJ.wsgi.application'




DATABASES = {
 'default': {
        'ENGINE': 'django.db.backends.dummy',
        'NAME': '',
        'USER': '',
'PASSWORD': '',
'HOST': '55.181.26.33', //mongodb ip address
'PORT': '27017',
    }
}



LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True
USE_L10N = True

USE_TZ = True


MEDIA_ROOT = os.path.join(PROJECT_ROOT, '..', 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = os.path.join(PROJECT_ROOT, '..', 'static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
        os.path.join(PROJECT_ROOT, 'static'),
)


TEMPLATE = (
 os.path.join(PROJECT_ROOT, 'templates'),
)

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

AUTH_USER_MODEL = 'mongo_auth.MongoUser'
MONGOENGINE_USER_DOCUMENT = 'mongoengine.django.auth.User'
MONGO_DATABASE_NAME = 'LJ_blog'
from mongoengine import connect
connect(MONGO_DATABASE_NAME)

那么,我该如何解决导入错误?

1 个答案:

答案 0 :(得分:1)

Django支持已从0.10.0中的MongoEngine中删除。您可能可以使早期版本工作,但它可能不支持最新版本的Django。

使用mongoengine 0.10我们可以看到

  

/usr/lib/python2.7/site-packages/mongoengine/

不会有django包。 使用

安装mongoengine 0.9
  

sudo pip install mongoengine == 0.9

和django包(支持或扩展)将可用。