分离生产/开发设置时,Django ghost settings.py文件

时间:2018-04-23 01:04:30

标签: django

我在我的智慧'结束这个问题,我希望你们中的一个人有答案。回溯命中一个不存在的文件!

我有生产和开发Django应用程序 - 所以我试图利用manage.py上的设置参数来运行manage.pypython manage.py runserver --settings=athena.settings.development

时访问正确的设置

为了完成这项工作 - 我移动了我的settings.py文件,其中包含令人讨厌的if / else结构和一些打印语句,以确保设置仍然可以读取以下内容我到settings/base.py。我创建了一个设置目录,并移动/重命名了settings.py

athena/
 |-- athena/
 |    |-- __init__.py
 |    |-- settings/
 |    |    |-- __init__.py
 |    |    |-- base.py    <------
 |    |-- urls.py
 |    +-- wsgi.py
 +-- manage.py

manage.py无法读取的新settings/base.py - 默认为旧settings.py,因为您将在追溯中看到:

import os

from decouple import config

# BASE_DIR = os.path.dirname(os.path.dirname(__file__))
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print ('-----------')

# 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 = config('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config('DEBUG', cast=bool)

ALLOWED_HOSTS = ['athe.herokuapp.com',
                 '127.0.0.1']

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': {
            'debug': DEBUG,
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

# Application definition

INSTALLED_APPS = (
    'athena_app.apps.AthenaAppConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

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',
    'whitenoise.middleware.WhiteNoiseMiddleware',
)

ROOT_URLCONF = 'athena.urls'

WSGI_APPLICATION = 'athena.wsgi.application'


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

if os.environ['PROD_DB_NAME'] in os.environ():
    print ('-----------------base')
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': config('PROD_DB_NAME'),
            'USER': config('PROD_DB_USER'),
            'PASSWORD': config('PROD_DB_PASSWORD'),
            'HOST': config('PROD_DB_HOST'),
            'PORT': config('PROD_DB_PORT')
        }
    }
else:
    print ('------------------BASE')
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': os.environ['DEV_DB_NAME'],
            'USER': os.environ['DEV_DB_USERNAME'],
            'PASSWORD': os.environ['DEV_DB_PASSWORD'],
            'HOST': os.environ['DEV_DB_HOSTNAME'],
            'PORT': os.environ['DEV_DB_PORT'],
        }
    }

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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/New_York'

USE_I18N = True

USE_L10N = True

USE_TZ = False

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.9/howto/static-files/
# python athena/manage.py collectstatic --noinput

# destination of collectstatic command
STATIC_ROOT = 'https://athena-heroku.herokuapp.com/static/athena_app'
# STATIC_ROOT = os.path.join(BASE_DIR, "athena_app/static/athena_app")
STATIC_URL = '/static/'
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'athena_app/static/athena_app'),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

LOGIN_URL = '/login'
LOGOUT_REDIRECT_URL = '/home'

这是追溯:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users//ct-venv/ct-webapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "/Users//ct-venv/ct-webapp/lib/python2.7/site-packages/django/core/management/__init__.py", line 307, in execute
    settings.INSTALLED_APPS
  File "/Users//ct-venv/ct-webapp/lib/python2.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
    self._setup(name)
  File "/Users//ct-venv/ct-webapp/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users//ct-venv/ct-webapp/lib/python2.7/site-packages/django/conf/__init__.py", line 110, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Users//anaconda/lib/python2.7/importlib/__init__.py", line 37, in import_module
    __import__(name)
  File "/Users//other_projects/database/athena/athena/settings.py", line 96, in <module>
  File "/Users//ct-venv/ct-webapp/lib/python2.7/UserDict.py", line 40, in __getitem__
    raise KeyError(key)
KeyError: 'DEV_DB_USERNAME'

回溯是指不存在的设置文件!它指的是一个已被移动的文件。我试图在我的DJANGO_SETTINGS_MODULE中对wsgi.py manage.py以及unset.envpython manage.py runserver --settings=athena.settings.development的引用发表评论,但无济于事。我需要能够在前进之前更改参考。 python manage.py runserver --settings=athena.settings和{{1}}获得相同的结果。

0 个答案:

没有答案