From a6479f1f096d10b5163b00b5920a4249185090f9 Mon Sep 17 00:00:00 2001 From: Rens Houben Date: Mon, 25 Aug 2025 17:54:19 +0200 Subject: [PATCH] Proper .env loading and a working .gitignore --- src/config/settings.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/config/settings.py b/src/config/settings.py index 74cf790..2dfbd62 100644 --- a/src/config/settings.py +++ b/src/config/settings.py @@ -10,22 +10,18 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/5.2/ref/settings/ """ +import os from pathlib import Path from dotenv import load_dotenv + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent + load_dotenv(BASE_DIR / ".env") -# Quick-start development settings - unsuitable for production -# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/ - -# SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = os.environ["DJANGO_SECRET_KEY"] - -# SECURITY WARNING: don't run with debug turned on in production! DEBUG = int(os.environ.get("DJANGO_DEBUG", default=0)) - ALLOWED_HOSTS = os.environ.get( "DJANGO_ALLOWED_HOSTS", default="localhost 127.0.0.1 0.0.0.0" ).split() @@ -71,18 +67,26 @@ TEMPLATES = [ WSGI_APPLICATION = "config.wsgi.application" - -# Database -# https://docs.djangoproject.com/en/5.2/ref/settings/#databases +DB_ENGINE=os.environ.get('DB_ENGINE', default='sqlite3') +DEFAULT_DB = { + 'ENGINE': 'django.db.backends.%s' % DB_ENGINE, +} +if DB_ENGINE=='sqlite3': + DEFAULT_DB['NAME'] = os.path.join('BASE_DIR', os.environ.get('DB_NAME', default='urania.sqlite3')) +else: + DEFAULT_DB['NAME'] = os.environ.get('DB_NAME') + DEFAULT_DB['USER'] = os.environ.get('DB_USER') + DEFAULT_DB['PASSWORD'] = os.environ.get('DB_PASSWORD') + DEFAULT_DB['HOST'] = os.environ.get('DB_HOST') + if DB_ENGINE=='postgresql': + DEFAULT_DB['PORT'] = os.environ.get('DB_PORT', default=5432) + elif DB_ENGINE=='mysql': + DEFAULT_DB['PORT'] = os.environ.get('DB_PORT', default=3306) DATABASES = { - "default": { - "ENGINE": "django.db.backends.sqlite3", - "NAME": BASE_DIR / "db.sqlite3", - } + 'default': DEFAULT_DB, } - # Password validation # https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators