我有一个部署到 Heroku 的 Django 项目,我正在尝试配置设置文件以在生产和开发之间更改数据库。
我真的不明白创建三个设置文件(包括一个基本文件)的意义,因为我唯一要更改的是数据库设置,但我不想使用
python manage.py runserver --settings=rivendell.settings.local
每次我运行manage.py。如果我在没有设置标志的情况下运行服务器运行但我得到一个错误:
settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.
我知道为什么会这样,但我可以设置 Django 来选择正确的文件。
我的树:
|-- rivendell
| |-- __init__.py
| |-- asgi.py
| |-- settings
| | |-- __init__.py
| | |-- consts
| | |-- local.py
| | |-- settings.py
| |-- urls.py
| `-- wsgi.py
我的设置文件:
import os
import dj_database_url
import dotenv
import environ
from .consts.apps import INSTALLED_APPS, MIDDLEWARE
from .consts.consts import (PASSWORD_VALIDATORS, POSTGRESQL_CONNECTION, STATIC,
TEMPLATES, Config, Language)
from .consts.logging import LOGGING
from .consts.rest_framework import REST_FRAMEWORK
env = environ.Env(DEBUG=(bool, False))
environ.Env.read_env()
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
env_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(env_file):
file = dotenv.load_dotenv(env_file)
SECRET_KEY = '+@sxt&+q5=9-u&it^5n-n!!a-2*fcbur^-umoxw*#!_ik$@kbc'
DEBUG = env('DEBUG')
ALLOWED_HOSTS = Config.HOSTS
INTERNAL_IPS = ['127.0.0.1', ]
INSTALLED_APPS = INSTALLED_APPS
REST_FRAMEWORK = REST_FRAMEWORK
MIDDLEWARE = MIDDLEWARE
ROOT_URLCONF = Config.URLS_PATH
TEMPLATES = TEMPLATES
WSGI_APPLICATION = Config.WSGI
DATABASES = {'default': dj_database_url.config(conn_max_age=600, ssl_require=True)} # My issue
AUTH_PASSWORD_VALIDATORS = PASSWORD_VALIDATORS
AUTH_USER_MODEL = 'users.CustomUser'
DJOSER = {
'SERIALIZERS': {
'user_create': 'users.serializers.UserRegistrationSerializer',
},
}
# Language and Time Zone
LANGUAGE_CODE = Language.LANGUAGE_CODE
TIME_ZONE = Language.TIME_ZONE
USE_I18N = True
USE_L10N = False
USE_TZ = True
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = STATIC
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
LOGGING = LOGGING
我的本地文件:
from .settings import *
DATABASES = {
'default': {
'ENGINE': POSTGRESQL_CONNECTION['ENGINE'],
'NAME': POSTGRESQL_CONNECTION['DATABASE'],
'user': POSTGRESQL_CONNECTION['USER'],
'PASSWORD': POSTGRESQL_CONNECTION['PASSWORD'],
'HOST': POSTGRESQL_CONNECTION['HOST'],
}
}