Setup Local Environment Variables
During development I'll likely end up with a bunch of third-party secrets. The .env
file with plaintext KEY=VALUE
formatting is the traditional source of truth for these sensitive artifacts but these won't be saved to the repository, prompting the need for local secrets management.
Django
ENV_NAME
default: dev
, may be either dev
, test
, or prod
.
Toggles the settings to use as employed in various environments:
- local dev
fly.toml
compose.yml
DJANGO_DEBUG
default: not set; intended to override DEBUG
in settings.
env | state |
---|---|
dev |
Always True |
test |
default: False , but overrideable via DJANGO_DEBUG . |
prod |
Always False |
DJANGO_SECRET_KEY
default: random; must be explicitly set in test
and prod
environments.
This is what populates SECRET_KEY
.
Copy/paste the following snippet to generate your key in the console.
python -c 'from django.core.management.utils import get_random_secret_key; print(get_random_secret_key())'
Add the generated value to the .env file:
- Must enclose in double quotes to prevent escaping characters.
DJANGO_ALLOWED_HOSTS
This is what populates ALLOWED_HOSTS
.
Database
DATABASE_URL
Settings Modified
TLDR: if the connection string DATABASE_URL
is sqlite-like use this engine django.db.backends.sqlite3
;
otherwise, use this engine: django.db.backends.postgresql
Overview
This environment variable is adopted by convention even if it doesn't appear anywhere in the official Django documentation (as of Django 4.2).
It refers to the connection of the server (handling the request-response cycle involving html
, css
, and js
) to a database management system which in turn is governed by sql
.
flowchart LR
subgraph db
sqlite
postgresql
end
django--DATABASE_URL--->db
Django supports many database engines but I prefer one of two: sqlite and postgresql.
Conventions
The URL is a connection string with specific conventions applied by Jazzband's dj_database_url
library. Listed here are the formats necessary for sqlite and postgresql:
Engine | Django Backend | Database URL Format |
---|---|---|
postgres | django.db.backends.postgresql |
postgres://USER :PASSWORD @HOST :PORT /NAME |
sqlite | django.db.backends.sqlite3 |
sqlite:///PATH |
Custom function
EMAIL_RECIPIENT
The boilerplate contains a pages
app which hosts a contact form. This form enables the visitor to submit a message whichi is emailed to the address contained in EMAIL_RECIPIENT
. If this is not set, a placeholder is provided but this will not be sufficient when ENV_NAME
is either test
or prod
.
/.env | |
---|---|
EMAIL_SENDER
The boilerplate makes use of transactional emails by virtue of django-allauth
and django.core.email.send_mail()
. When ENV_NAME
is test or prod, a valid email sender implies a pre-configured email server. I've already setup an email address that is verified by Postmark to handle the same. If this is not set, a placeholder is provided but this will not be sufficient when ENV_NAME
is either test
or prod
.
/.env | |
---|---|
DEFAULT_FROM_EMAIL
Affects the way the email address from the sender is viewed when using Postmark.
When passing .env containing a space, e.g. "start-django donotreply@mv3.dev" use double quotes