From 93f577cb9cac63fcbaaec9ae411b598e7f848f8d Mon Sep 17 00:00:00 2001 From: Armin Patel <72291325+arminpatel@users.noreply.github.com> Date: Sat, 30 Dec 2023 11:53:56 +0530 Subject: [PATCH] separate settings for prod and dev --- backend/core/{settings.py => settings/base.py} | 13 +------------ backend/core/settings/development.py | 10 ++++++++++ backend/core/settings/production.py | 8 ++++++++ backend/manage.py | 7 ++++++- 4 files changed, 25 insertions(+), 13 deletions(-) rename backend/core/{settings.py => settings/base.py} (89%) create mode 100644 backend/core/settings/development.py create mode 100644 backend/core/settings/production.py diff --git a/backend/core/settings.py b/backend/core/settings/base.py similarity index 89% rename from backend/core/settings.py rename to backend/core/settings/base.py index b1e938c..63169e6 100644 --- a/backend/core/settings.py +++ b/backend/core/settings/base.py @@ -1,7 +1,6 @@ import os from pathlib import Path from dotenv import load_dotenv -import dj_database_url # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -15,7 +14,7 @@ SECRET_KEY = os.environ.get('DJANGO_SECRET', 'default') # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = os.environ.get('DJANGO_DEBUG', "False").lower == 'true' +DEBUG = os.environ.get('DEBUG') != 'False' ALLOWED_HOSTS = ['*'] # ALLOWED_HOSTS = os.environ.get('DJANGO_ALLOWED_HOSTS').split(' ') @@ -92,16 +91,6 @@ # Database # https://docs.djangoproject.com/en/4.2/ref/settings/#databases -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': BASE_DIR / 'db.sqlite3', - # dj_database_url.config(default=os.environ.get('DATABASE_URL')) - # dj_database_url.parse(os.environ.get("DATABASE_URL")) - } -} - -DATABASES['default'] = dj_database_url.parse(os.environ.get("DATABASE_URL")) # Password validation # https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators diff --git a/backend/core/settings/development.py b/backend/core/settings/development.py new file mode 100644 index 0000000..045056d --- /dev/null +++ b/backend/core/settings/development.py @@ -0,0 +1,10 @@ +from .base import * # noqa: F403, F401 + +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + # dj_database_url.config(default=os.environ.get('DATABASE_URL')) + # dj_database_url.parse(os.environ.get("DATABASE_URL")) + } +} diff --git a/backend/core/settings/production.py b/backend/core/settings/production.py new file mode 100644 index 0000000..3ec3f4a --- /dev/null +++ b/backend/core/settings/production.py @@ -0,0 +1,8 @@ +import os + +from .base import * # noqa: F403, F401 +import dj_database_url + +DATABASES = { + 'default': dj_database_url.config(default=os.environ.get('DATABASE_URL')) +} diff --git a/backend/manage.py b/backend/manage.py index f2a662c..31c7be1 100755 --- a/backend/manage.py +++ b/backend/manage.py @@ -3,10 +3,15 @@ import os import sys +from core.settings import base + def main(): """Run administrative tasks.""" - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings') + if base.DEBUG: + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings.development') + else: + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings.production') try: from django.core.management import execute_from_command_line except ImportError as exc: