如何在没有默认数据库设置的情况下定义Django设置?

时间:2016-09-14 06:39:51

标签: django

根据Django Docs,我们可以像这样{}定义空的django设置,但它仍会引发错误。

在/处配置错误 settings.DATABASES配置不正确。请提供ENGINE值。检查设置文档以获取更多详细信息。

"""
Django settings for impactFund project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/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__))
PROJECT_PATH = os.path.split(os.path.abspath(os.path.dirname(__file__)))[0]


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 's^(gvcg1_!!!8wnpikp[hj4+%ypay1$zsf54=j6o3cuuxigjxiscqno'

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

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'Ken_app',
    'app',

    'captcha',
    'django.contrib.sites',
    'treebeard',
    'djangocms_text_ckeditor',    
    'cms',  # django CMS itself
    'mptt',  # utilities for implementing a modified pre-order traversal tree
    'menus',  # helper for model independent hierarchical website navigation
    #'south',  # intelligent schema and data migrations
    'sekizai',  # for javascript and css management
    'djangocms_admin_style',  # for the admin skin. You **must** add 'djangocms_admin_style' in the list **before** 'django.contrib.admin'.
     # to enable messages framework (see :ref:`Enable messages <enable-messages>`)

)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.gzip.GZipMiddleware',

    'cms.middleware.user.CurrentUserMiddleware',
    'cms.middleware.page.CurrentPageMiddleware',
    'cms.middleware.toolbar.ToolbarMiddleware',
    'cms.middleware.language.LanguageCookieMiddleware',    

)

ROOT_URLCONF = 'Fundimpact.urls'

WSGI_APPLICATION = 'Fundimpact.wsgi.application'


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


DATABASES = {

    'default': {  # Leave this blank, we do not want a 'default' database defined.
        'ENGINE': '', 
        'NAME': '',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    },


    'prisen_db':{
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'prisen_db',                      
        'USER': 'prmken_user',                      
        'HOST': '59.77.142', 
        'PASSWORD':'PmN7&#5B',    

    },
    'prism_db':{
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'prism_db',                      
        'USER': 'prism_user',                      
        'HOST': '59.77.142', 
        'PASSWORD':'PC$MN',    

    }
 }   



DATABASE_ROUTERS = ['app.dbRouter.AppDBRouter', 'Ken_app.dbRouter.Ken_appDBRouter']

TEMPLATES = [
   {
       'BACKEND': 'django.template.backends.django.DjangoTemplates',
       'DIRS': [
       os.path.join(PROJECT_PATH, 'Ken_app','template'),
       os.path.join(PROJECT_PATH, 'app','templates'),
                ],
       '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',
               'django.core.context_processors.i18n',              
               'django.core.context_processors.static',
               'sekizai.context_processors.sekizai',
           ],
       },
   },
]


# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

#FILE_CHARSET = ""

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)



CKEDITOR_SETTINGS = {
    'language': '{{ language }}',
    'toolbar': 'CMS',
    'skin': 'moono',
}


CMS_PLACEHOLDER_CONF = {
    'content': {
        'plugins': ['TextPlugin', 'LinkPlugin'],
        'default_plugins':[
            {
                'plugin_type':'TextPlugin',
                'values':{
                    'body':'<p>Great websites : %(_tag_child_1)s and %(_tag_child_2)s</p>'
                },
                'children':[
                    {
                        'plugin_type':'LinkPlugin',
                        'values':{
                            'name':'django',
                            'url':'https://www.djangoproject.com/'
                        },
                    },
                    {
                        'plugin_type':'LinkPlugin',
                        'values':{
                            'name':'django-cms',
                            'url':'https://www.django-cms.org'
                        },
                    },
                ]
            },
        ]
    }
}

这些是我的路由器,适用于个人应用文件夹中的两个应用

import random

class Ken_appDBRouter(object):
    """
    A router to control app db operations
    """
    def db_for_read(self, model, **hints):
        "Point all operations on app models to 'kenya"
        from django.conf import settings
        if not settings.DATABASES.has_key('Ken_app'):
            return None
        if model._meta.app_label == 'Ken_app':
            return 'prisen_db'
        return None

    def db_for_write(self, model, **hints):
        "Point all operations on app1 models to 'kenya'"
        from django.conf import settings
        if not settings.DATABASES.has_key('Ken_app'):
            return None
        if model._meta.app_label == 'Ken_app':
            return 'prisen_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        "Allow any relation if a model in app is involved"
        from django.conf import settings
        if not settings.DATABASES.has_key('Ken_app'):
            return None
        if obj1._meta.app_label == 'Ken_app' or obj2._meta.app_label == 'Ken_app':
            return True
        return None

    def allow_syncdb(self, db, model):
        "Make sure the Ken_app app only appears on the 'kenya' db"
        from django.conf import settings
        if not settings.DATABASES.has_key('Ken_app'):
            return None
        if db == 'prisen_db':
            return model._meta.app_label == 'Ken_app'
        elif model._meta.app_label == 'Ken_app':
            return False
        return None


import random

class AppDBRouter(object):
    """
    A router to control app db operations
    """
    def db_for_read(self, model, **hints):
        "Point all operations on app models to 'app'"
        from django.conf import settings
        if not settings.DATABASES.has_key('app'):
            return None
        if model._meta.app_label == 'app':
            return 'prism_db'
        return None

    def db_for_write(self, model, **hints):
        "Point all operations on app1 models to 'india'"
        from django.conf import settings
        if not settings.DATABASES.has_key('app'):
            return None
        if model._meta.app_label == 'app':
            return 'prism_db'
        return None

    def allow_relation(self, obj1, obj2, **hints):
        "Allow any relation if a model in app is involved"
        from django.conf import settings
        if not settings.DATABASES.has_key('app'):
            return None
        if obj1._meta.app_label == 'app' or obj2._meta.app_label == 'app':
            return True
        return None

    def allow_syncdb(self, db, model):
        "Make sure the app app only appears on the 'app' db"
        from django.conf import settings
        if not settings.DATABASES.has_key('app'):
            return None
        if db == 'prism_db':
            return model._meta.app_label == 'app'
        elif model._meta.app_label == 'app':
            return False
        return None

这是追溯。 enter image description here

2 个答案:

答案 0 :(得分:1)

您的default数据库不为空:

'default': {  # Leave this blank, we do not want a 'default' database defined.
    'ENGINE': '', 
    'NAME': '',
    'USER': '',
    'PASSWORD': '',
    'HOST': '',
    'PORT': '',
},

将其更改为

'default': {},

此外,您缺少auth的路由。更改其中一个路由器也接受auth或为其添加第三个路由器。

答案 1 :(得分:0)

超越内部检查机制:

# We wont use Django databases
DATABASES = {
    'default': {
        'ENGINE': '',
    }
}

# Override TestRunner methods
TEST_RUNNER = 'tests.NoSQLTestRunner'

您将创建另一个tests.py文件并保存下面的内容

from django.test.simple import DjangoTestSuiteRunner
from django.test import TestCase

class NoSQLTestRunner(DjangoTestSuiteRunner):
    def setup_databases(self):
        pass
    def teardown_databases(self, *args):
        pass

class NoSQLTestCase(TestCase):
    def _fixture_setup(self):
        pass
    def _fixture_teardown(self):
        pass