0

I have a Django project deployed to Heroku, where I'm trying to configure the settings file to change DB between production and development.

I don't really see the point of creating three settings file (including a base file) since the only thing I'm changing is the database settings but I don't want to use

python manage.py runserver --settings=rivendell.settings.local

every time I run manage.py. if I run without the settings flag the server runs But I get an error:

settings.DATABASES is improperly configured. Please supply the ENGINE value. Check settings documentation for more details.

I know why this is happening but I can set Django to choose the right file.

My tree:

|-- rivendell
|   |-- __init__.py
|   |-- asgi.py
|   |-- settings
|   |   |-- __init__.py
|   |   |-- consts
|   |   |-- local.py
|   |   |-- settings.py
|   |-- urls.py
|   `-- wsgi.py

my settings file:

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

my local file:

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'],
    }
}

  • Are these your real secret keys and credentials? – Klaus D. Aug 04 '20 at 11:36
  • No, random generated string and email address I added. –  Aug 04 '20 at 11:37
  • @yovel cohen you need to switch between 2 DB in you code? – Zaraki Kenpachi Aug 04 '20 at 11:55
  • @ZarakiKenpachi well, I need to use the local DB settings (in local file) when running manage.py locally without the flag and using the DJ package in production, the reason I don't want to use the flag is I want to create a generic sulotion that when another developer will join the project they won't have to use the flag or get confused by the error. –  Aug 04 '20 at 13:08
  • @yovel cohen if you post code with git then add `settings.py` in `.gitignore` file – Zaraki Kenpachi Aug 04 '20 at 13:12
  • do you mean local? because settings.py is my production file, if I do that then how would my manage.py and wsgi.py be able to get my settings file? –  Aug 04 '20 at 14:05

0 Answers0