Skip to content

Commit

Permalink
Merge pull request #1005 from cquinn540/django-local-env
Browse files Browse the repository at this point in the history
Switch from container to local with env variable
  • Loading branch information
andrewtavis authored Nov 13, 2024
2 parents 44c7722 + c8e9d5f commit d34f95a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
1 change: 1 addition & 0 deletions .env.dev.local
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_HOST="localhost"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ frontend/.github/
.env
.env.*
!.env.dev
!.env.dev.local
dist

# Node dependencies
Expand Down
9 changes: 4 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 8 additions & 2 deletions backend/backend/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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/

Expand Down

0 comments on commit d34f95a

Please sign in to comment.