0

I am using Django in conjunction with MongoDB using mongoengine. I was following the tutorial on https://docs.djangoproject.com/en/1.7/intro/tutorial02/ and therefore wanted to create a superuser and log in. Creation worked without problem appearantly. However when I tried to log in, I was greeted by the following site:

enter image description here

I suppose this has to do with my initialization of the database using the dummy database in the settings.py. However I was told using mongoengine requires to do it this way and it did not cause problems earlier. Anyway: here is the content of my settings.py

"""
Django settings for myproject 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 = 'xg0wnp^w)i@svh13#^v45**4^3v-at#ktre=^n#cw2!6(q__gq'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition


INSTALLED_APPS = (
    'django.contrib.auth',
    'mongoengine.django.mongo_auth',
    'django.contrib.admin',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)


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',
)

ROOT_URLCONF = 'myproject.urls'

WSGI_APPLICATION = 'myproject.wsgi.application'

import mongoengine

# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

#DATABASES = {
#   'default' : {
#      'ENGINE' : 'django_mongodb_engine',
#      'NAME' : 'my_database'
#   }
#}

#DATABASES = {
#    'default': {
#        'ENGINE': 'django.db.backends.sqlite3',
#        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#    }
#}

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.dummy',
    }
}

mongoengine.connect(db='local', alias ='default')

#from django.contrib.auth import get_user_model
#user = get_user_model().objects.create_user(**user_data)

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

AUTH_USER_MODEL = 'mongo_auth.MongoUser'
MONGOENGINE_USER_DOCUMENT = 'mongoengine.django.auth.User'

#MONGOENGINE_USER_DOCUMENT = 'mongoengine.django.auth.User'


#SESSION_ENGINE = 'mongoengine.django.sessions'
#SESSION_SERIALIZER = 'mongoengine.django.sessions.BSONSerializer'

#_MONGODB_USER = 'mongouser'
#_MONGODB_PASSWD = 'password'
#_MONGODB_HOST = 'thehost'
#_MONGODB_NAME = 'thedb'
#_MONGODB_DATABASE_HOST = \
#    'mongodb://%s:%s@%s/%s' \
#    % (_MONGODB_USER, _MONGODB_PASSWD, _MONGODB_HOST, _MONGODB_NAME)

#mongoengine.connect(_MONGODB_NAME, host=_MONGODB_DATABASE_HOST)

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

# 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/'
Hagadol
  • 362
  • 1
  • 3
  • 16

2 Answers2

0

Kindly configure the following settings in setting.py file

import mongoengine
DATABASES = {
    'default': {
        'ENGINE': '',
    },
}

SESSION_ENGINE = 'mongoengine.django.sessions' # optional

_MONGODB_USER = 'mongouser'
_MONGODB_PASSWD = 'password'
_MONGODB_HOST = 'thehost'
_MONGODB_NAME = 'thedb'
_MONGODB_DATABASE_HOST = \
    'mongodb://%s:%s@%s/%s' \
    % (_MONGODB_USER, _MONGODB_PASSWD, _MONGODB_HOST, _MONGODB_NAME)

mongoengine.connect(_MONGODB_NAME, host=_MONGODB_DATABASE_HOST)

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

I hope the above solution will resolve your issue.

Hagadol
  • 362
  • 1
  • 3
  • 16
jademaddy
  • 99
  • 7
  • Thanks for the quick answer, but appearantly this did not fix everything. I think this has to do with the fact that my database is named differently. Maybe I need to adjust the code you have given me. When trying to start the server I now get the error message: raise ConnectionError("Cannot connect to database %s :\n%s" % (alias, e)) mongoengine.connection.ConnectionError: Cannot connect to database default : [Errno 11001] getaddrinfo failed – Hagadol Dec 24 '14 at 11:13
  • hmm... it does not fix anything. – Saad Bin Shahid Oct 18 '15 at 21:49
0

what to do if database-engine not needed

default database is used by some of the default authentication APIs used in Middleware-Apps.

try to make your MIDDLEWARE_APPS tuple as follows:

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',
)

I am just a beginner, so if any more errors come around please do comment.

This solution breaks the default Admin Panel backend provided by Django. I'm trying to find a work around for the same.

Redirecting to a different view in urls.py for admin page may help though

Community
  • 1
  • 1
phoenisx
  • 1,507
  • 17
  • 28