2017-07-06 52 views
2

我使用的Django 1.8和mongoengine 但提出申請,當我嘗試配置的Django setting.py文件使用一個虛擬的數據庫,我收到以下錯誤Django1.8和Mongoclient settings.DATABASES配置不正確。

(orahienv) [email protected]:/var/www/html/admin_python$ python manage.py runserver 
Performing system checks... 

System check identified no issues (0 silenced). 
July 06, 2017 - 16:57:25 
Django version 1.8, using settings 'admin_python.settings' 
Starting development server at http://127.0.0.1:8000/ 
Quit the server with CONTROL-C. 
Performing system checks... 

System check identified no issues (0 silenced). 
July 06, 2017 - 17:00:15 
Django version 1.8, using settings 'admin_python.settings' 
Starting development server at http://127.0.0.1:8000/ 
Quit the server with CONTROL-C. 
<WSGIRequest: GET '/auth/login/?next=/'> 
Internal Server Error: /auth/login/ 
Traceback (most recent call last): 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 164, in get_response 
    response = response.render() 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/template/response.py", line 158, in render 
    self.content = self.rendered_content 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/template/response.py", line 135, in rendered_content 
    content = template.render(context, self._request) 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/template/backends/django.py", line 74, in render 
    return self.template.render(context) 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/template/base.py", line 208, in render 
    with context.bind_template(self): 
    File "/usr/lib/python2.7/contextlib.py", line 17, in __enter__ 
    return self.gen.next() 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/template/context.py", line 235, in bind_template 
    updates.update(processor(self.request)) 
    File "/var/www/html/admin_python/admin_app/context_processors.py", line 30, in init_menu 
    if request.user.is_authenticated(): 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/utils/functional.py", line 226, in inner 
    self._setup() 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/utils/functional.py", line 359, in _setup 
    self._wrapped = self._setupfunc() 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/contrib/auth/middleware.py", line 22, in <lambda> 
    request.user = SimpleLazyObject(lambda: get_user(request)) 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/contrib/auth/middleware.py", line 10, in get_user 
    request._cached_user = auth.get_user(request) 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 174, in get_user 
    user = backend.get_user(user_id) 
    File "/var/www/html/admin_python/admin_app/backend.py", line 29, in get_user 
    return TblAdmin.objects.get(pk=user_id) 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method 
    return getattr(self.get_queryset(), name)(*args, **kwargs) 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/models/query.py", line 328, in get 
    num = len(clone) 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/models/query.py", line 144, in __len__ 
    self._fetch_all() 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/models/query.py", line 965, in _fetch_all 
    self._result_cache = list(self.iterator()) 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/models/query.py", line 238, in iterator 
    results = compiler.execute_sql() 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql 
    sql, params = self.as_sql() 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 367, in as_sql 
    extra_select, order_by, group_by = self.pre_sql_setup() 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 48, in pre_sql_setup 
    self.setup_query() 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 39, in setup_query 
    self.select, self.klass_info, self.annotation_col_map = self.get_select() 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 216, in get_select 
    ret.append((col, self.compile(col, select_format=True), alias)) 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 346, in compile 
    sql, params = node.as_sql(self, self.connection) 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/models/expressions.py", line 611, in as_sql 
    return "%s.%s" % (qn(self.alias), qn(self.target.column)), [] 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 337, in quote_name_unless_alias 
    r = self.connection.ops.quote_name(name) 
    File "/var/www/html/admin_python/orahienv/local/lib/python2.7/site-packages/django/db/backends/dummy/base.py", line 21, in complain 
    raise ImproperlyConfigured("settings.DATABASES is improperly configured. " 
ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details. 
[06/Jul/2017 17:00:21]"GET /auth/login/?next=/ HTTP/1.1" 500 206077 

我認識的人問類似的問題之前, 。但這都是關於發動機的價值。是否有人知道我如何使用NAME值解決這個錯誤?

setting.py -

import os 
import admin_app 
from django.utils import timezone 
import mongoengine 
"""from mongoengine import connect 
connect('pom')""" 

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.8/howto/deployment/checklist/ 

# SECURITY WARNING: keep the secret key used in production secret! 
SECRET_KEY = 'uj8$6(ol=!w)b3-luqzb=#6(j19gkbrnp2asq6=xt%5*s2ylz(' 

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

ALLOWED_HOSTS = [] 

SPATIALITE_LIBRARY_PATH = 'mod_spatialite' 
# Application definition 

INSTALLED_APPS = (
    'django.contrib.admin', 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    'rest_framework', 
    'admin_app', 
    'celery', #http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#using-the-django-orm-cache-as-a-result-backend 
) 

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', 
    'djangoflash.middleware.FlashMiddleware', 
    #'django.middleware.cache.CacheMiddleware', #http://stackoverflow.com/a/658583/2546013 
) 

ROOT_URLCONF = 'admin_python.urls' 

TEMPLATES = [ 
    { 
     'BACKEND': 'django.template.backends.django.DjangoTemplates', 
     'DIRS': [os.path.join(BASE_DIR, 'templates'), os.path.join(BASE_DIR, 'media')], 
     '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', 
       'admin_app.context_processors.init_menu', 
       'admin_app.context_processors.server_urls', 
       'djangoflash.context_processors.flash', 
      ], 
     }, 
    }, 
] 


STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
) 
STATIC_ROOT = os.path.join(BASE_DIR, "static/") 
MEDIA_URL = '/media/' 
HOST_URL='http://127.0.0.1:8001/media/' 
MEDIA_ROOT = os.path.join(BASE_DIR, "media/") 
WSGI_APPLICATION = 'admin_python.wsgi.application' 
STATIC_URL = '/static/' 
SERVER_URL = 'http://localhost:8001/' 
WEBAPP_URL = 'http://localhost/webapp_angular/' 
USERAPI_URL = 'http://localhost:8000/' 
HOST_URL_NEW='http://localhost:8001/media/' 


# Database 
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases 
""" 
DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.mysql', 
     'NAME': 'admin_db', 
     'USER': 'root', 
     'PASSWORD': 'root', 
     'HOST': 'localhost', 
    } 
}""" 

"""DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.dummy', 
    } 
} 
""" 
""" 
DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.dummy', 
     'NAME': 'pom', 
     'USER': 'root', 
     'PASSWORD': 'root', 
     'HOST': 'localhost', 
    } 
} 
from mongoengine import connect 
connect('pom', username='root', password='root')""" 
DATABASES = { 
    'default': { 
     'ENGINE': '', 
    }, 
} 


_MONGODB_USER = 'root' 
_MONGODB_PASSWD = 'root' 
_MONGODB_HOST = 'localhost' 
_MONGODB_NAME = 'pom' 
_MONGODB_DATABASE_HOST = \ 
    'mongodb://%s:%[email protected]%s/%s' \ 
    % (_MONGODB_USER, _MONGODB_PASSWD, _MONGODB_HOST, _MONGODB_NAME) 

mongoengine.connect(_MONGODB_NAME, host=_MONGODB_DATABASE_HOST) 
# Internationalization 
# https://docs.djangoproject.com/en/1.8/topics/i18n/ 


LANGUAGE_CODE = 'en-us' 

TIME_ZONE = 'Asia/Kolkata' 

USE_I18N = True 

USE_L10N = True 

USE_TZ = False 


# Static files (CSS, JavaScript, Images) 
# https://docs.djangoproject.com/en/1.8/howto/static-files/ 


REST_FRAMEWORK = { 
    # other settings... 

    'DEFAULT_AUTHENTICATION_CLASSES': [], 
    'DEFAULT_PERMISSION_CLASSES': [], 
} 
LOGIN_URL = '/auth/login/' 
LOGIN_REDIRECT_URL = '/' 

AUTHENTICATION_BACKENDS = ( 
    'admin_app.backend.AuthBackend', 
    'django.contrib.auth.backends.ModelBackend', 

) 

AUTH_USER_MODEL = 'admin_app.TblAdmin' 

EMAIL_HOST = 'email-smtp.us-east-1.amazonaws.com' 
EMAIL_PORT = 587 
EMAIL_HOST_USER = 'AKIAJB6VACOVZANVTC5A' 
EMAIL_HOST_PASSWORD = 'Atu9qbV+X5aiwIlQzVAOVjU/AxkxMDV3X0l0FLWweSn9' 
EMAIL_USE_TLS = True 
DEFAULT_FROM_EMAIL = '[email protected]' 

#LOGGING SETTING 
LOGGING = { 
    'version': 1, 
    'disable_existing_loggers': False, 
    'formatters': { 
     'verbose': { 
      'format' : "[%(asctime)s] %(levelname)s [%(filename)s:%(funcName)s:%(lineno)s] %(message)s", 
      'datefmt' : "%d/%b/%Y %H:%M:%S" 
     }, 
     'simple': { 
      'format': '%(levelname)s %(message)s' 
     }, 
    }, 
    'filters': { 
     'require_debug_false': { 
      '()': 'django.utils.log.RequireDebugFalse' 
     } 
    }, 
    'handlers': { 
     'mail_admins': { 
      'level': 'ERROR', 
      'filters': ['require_debug_false'], 
      'class': 'django.utils.log.AdminEmailHandler' 
     }, 
     'applogfile': { 
      'level':'DEBUG', 
      'class':'logging.FileHandler', 
      'filename': os.path.join(BASE_DIR, 'drinsta.log'), 
      'formatter': 'verbose' 
      }, 

    }, 
    'loggers': { 
     'django.request': { 
      'level': 'ERROR', 
      'propagate': True, 
      'handlers': ['applogfile',], 
     }, 
     'drinsta': { 
      'handlers': ['applogfile',], 
      'level': 'DEBUG', 
     }, 
    } 
} 

#https://docs.djangoproject.com/en/1.8/topics/http/sessions/#configuring-the-session-engine 
#https://github.com/dlrust/python-memcached-stats 
#We can use the following command to see the keys: 
# python -m memcached_stats 127.0.0.1 11211 
CACHES = { 
    'default': { 
     'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 
     'LOCATION': '127.0.0.1:11211', 
     'TIMEOUT' : 300 #Default 
    } 
} 
CACHE_BACKEND = 'memcached://127.0.0.1:11211/' 

SESSION_ENGINE= 'django.contrib.sessions.backends.cache' 

''' 
RabbitMQ configurations begins 
''' 
#OLD FORMAT: 
# BROKER_HOST = "127.0.0.1" 
# BROKER_PORT = 5672 # default RabbitMQ listening port 
# BROKER_USER = "admin" 
# BROKER_PASSWORD = "admin" 
# BROKER_VHOST = "admin" #Virtual hosts provide a way to segregate applications using the same RabbitMQ instance. Different users can have different access privileges to different vhost and queues and exchanges can be created so they only exists in one vhost. 

#NEW FORMAT: 
BROKER_URL = 'amqp://admin:[email protected]:5672/admin' 
''' 
RabbitMQ configurations ends 
''' 
''' 
Celery configurations begins 
''' 
#CELERY_BACKEND = "amqp" # telling Celery to report the results back to RabbitMQ 
#'amqp' means Advanced Message Queuing Protocol 
# CELERY_RESULT_DBURI = "" 
CELERY_TIMEZONE = "Asia/Kolkata" 
#CELERY_RESULT_BACKEND='djcelery.backends.cache:CacheBackend'  
CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend' 

CELERY_IMPORTS = ("tasks",) 
CELERY_RESULT_PERSISTENT = True #If set to True, result messages will be persistent. This means the messages will not be lost after a broker restart. The default is for the results to be transient. 
CELERY_TASK_RESULT_EXPIRES = None #Never delete the SUCCESSFUL tasks from the database. http://docs.celeryproject.org/en/latest/configuration.html#celery-task-result-expires 
# CELERY_RESULT_BACKEND = 'cache+memcached://127.0.0.1:11211/' 
# CELERY_CACHE_BACKEND = 'memory' 
#http://www.hiddentao.com/archives/2012/01/27/processing-long-running-django-tasks-using-celery-rabbitmq-supervisord-monit/ 
''' 
Celery configurations ends 
''' 

回答

2
connect(
    db='test', 
    username='user', 
    password='12345', 
    host='mongodb://admin:[email protected]/production' 
) 

,並更改settings.py

+0

編輯我的代碼,但仍然得到同樣的錯誤數據庫= {} 從mongoengine進口連接 連接( DB = '聚甲醛', 用戶名= '根', 密碼= '根', 主機=「localhost」的 ) –

+0

主機需要給予像我給什麼,用戶/本地主機 – Exprator

+0

我仍然得到同樣的錯誤連接( DB =「聚甲醛」, 用戶名=「管理員」, 密碼= 'root', host ='mongodb:// admin:root @ localhost/po m' ) –

2

DATABASESDATABASES = {}節。如果你不想因爲你使用mongoengine,而不是配置的數據庫,然後刪除DATABASES完全取決於您的設置,或將其更改爲空字典。

DATABASES = {} 

如果您在您的DATABASES字典的任何項目,那麼他們必須包括ENGINE,否則你會得到ImproperlyConfigured錯誤。

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', 
     ... 
    }, 
} 
+0

我得到同樣的錯誤空字典DATABASES = {}。和第二個選項的問題是,我有mongodb作爲數據庫是有效的使用sqllite3引擎? –

+0

我認爲它與引擎錯誤相關。其實我在這個項目中用mongodb數據庫替換sql,所以首先我試圖在django和mongodb數據庫之間建立連接,但是它給了我這個錯誤 –

+0

我[不確定mongoengine是否支持Django](https:// github.com/MongoEngine/django-mongoengine),所以我無法幫助。我只能說你的原始問題中的'ENGINE':'','絕對無效。 – Alasdair