diff --git a/.env.dev.local b/.env.dev.local new file mode 100644 index 000000000..be6a117c6 --- /dev/null +++ b/.env.dev.local @@ -0,0 +1 @@ +DATABASE_HOST="localhost" diff --git a/.gitignore b/.gitignore index fc1bf385b..e34fe82ea 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,7 @@ frontend/.github/ .env .env.* !.env.dev +!.env.dev.local dist # Node dependencies diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 43a2b65ad..04a659af7 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -238,15 +238,14 @@ Our backend depends on a connection to a postgres DB, therefore we need to setup docker compose --env-file .env.dev up db ``` -In order to connect to the DB, we need to change the `DATABASE_HOST` environment variable inside the `.env.dev` file first. +To run locally, set the environment variable `DJANGO_ENV` to `LOCAL_DEV`: ```bash -# Current -DATABASE_HOST=db -# Changed -DATABASE_HOST=localhost +export DJANGO_ENV=LOCAL_DEV ``` +When this is set, django will load environment variables from `env.dev` first, and then from `.env.dev.local` which will overwrite some variables for local development. + From here we need the project's dependencies, with the practice being to create a virtual environment first within your local activist directory and then install the dependencies within it: On Unix or MacOS, run: diff --git a/backend/backend/settings.py b/backend/backend/settings.py index 7faf23317..a5ff16cff 100644 --- a/backend/backend/settings.py +++ b/backend/backend/settings.py @@ -14,7 +14,14 @@ import django_stubs_ext import dotenv -dotenv.load_dotenv() +PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent + +if os.getenv("DJANGO_ENV") == "LOCAL_DEV": + dotenv.load_dotenv(override=True, dotenv_path=PROJECT_ROOT / ".env.dev") + dotenv.load_dotenv(override=True, dotenv_path=PROJECT_ROOT / ".env.dev.local") + +else: + dotenv.load_dotenv() # MARK: DB @@ -27,7 +34,6 @@ # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent - # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/