Sensible defaults
File structure
Root folder
<root>
├── .github/workflows/main.yml # ci
├── .vscode/ # configures mkdocs, ruff, pytest, etc., file associations
├── etc/ # example env variables
├── src/ # main project folder
├── config/ # project named config
├── settings/
├── __init__.py # switch env: dev | test | prod
├── _auth.py # django_allauth, postmark email
├── _settings.py # base settings
Default Secrets
/etc/env.example.0.dev
ENV_NAME= # If not set, will default to dev
DJANGO_DEBUG= # If not set, DEBUG will always be False
REDIS_URL=redis:// # If not set, will use Sqlite.Huey
DATABASE_URL=postgres://db_usr:pw@localhost:5432/db_pg # Default: /src/data/start.sqlite
DJANGO_SECRET_KEY= # If not set, will use a randomly generated secret when ENV_NAME is dev
DJANGO_ALLOWED_HOSTS= # If not set, will always use a fixed list
EMAIL_RECIPIENT= # If not set, will use default (used in contact form)
EMAIL_SENDER= # If not set, will use default (used in by transactional email service postmark)
DEFAULT_FROM_EMAIL= # If not set, will use default (used in by transactional email service postmark)
# APIs
POSTMARK_API_KEY= # If not set, Postmark will not work
GOOGLE_ID= # If not set, Google oAuth will not work with 127.0.0.1
GOOGLE_KEY= # If not set, Google oAuth will not work with 127.0.0.1
GITHUB_ID= # If not set, Github oAuth will not work with 127.0.0.1
GITHUB_KEY= # If not set, Github oAuth will not work with 127.0.0.1
CF_ACCT_ID=cloudflare-account # If not set, remote media storage of images will not work
CF_IMG_TOKEN=cloudflare-secret # If not set, remote media storage of images will not work
CF_IMG_HASH=cloudflare-hash # If not set, remote media storage of images will not work
Environment Switcher
This boilerplate's ENV_NAME
settings (dev
, test
, prod
) is shorthand for "toggle settings so app behaves like this for state dev
, etc.". To be clear on what I mean by test
and prod
in a hand-wavvy outline mode:
test
, prod
I use test
to denote that DEBUG=False
and now need to make sure that Django app can function as expected in the real world.
I'll toggle settings depending on need:
- use
gunicorn
orrunserver_plus
instead of runserver - try email service, e.g. from
django.core.mail.backends.console.EmailBackend
topostmark.django_backend.EmailBackend
- save images from MEDIA_ROOT (
/mediafiles
) in the file system to a remote solution like Cloudflare Images - Ensure social authentication works with
127.0.0.1
/localhost
or with containerized environments using0.0.0.0
- Since
DEBUG
here is adjustable and defaults toFalse
, need to explicitly state that DEBUG isTrue
for staticfiles to be served. See reference box - Compress static files to evaluate footprint in browser
- Host and database is now selected
- Considerations of authentication, consent and compliance are introduced
- Prevent runtime if missing first & third-party secret
- Apply security settings to local context
- Ensure
.env
file secrets now stored in more secure location for later reuse - Django now prepared for public use in live deployment
When ENV_NAME is dev or not set
# (1)
DEBUG = True
# (2)
SECRET_KEY = env("DJANGO_SECRET_KEY", get_random_secret_key())
# (3)
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
# (4)
ignore_warnings()
- When not explicitly declared
DEBUG
default isFalse
. - Will generate a random key if no environment variable
DJANGO_SECRET_KEY
is detected. - Overrides postmark
- Limit text being displayed in the terminal during development.
When ENV_NAME is test
# (1)
SECRET_KEY = env("DJANGO_SECRET_KEY")
# (2)
if DEBUG := env.bool("DJANGO_DEBUG", False):
# (3)
INTERNAL_IPS = ALLOWED_HOSTS
# (4)
check_auth()
# (5)
EMAIL_BACKEND = "postmark.django_backend.EmailBackend"
- If this
SECRET_KEY
not set, will error out - Enable
DEBUG
even onENV_NAME
test - Used by django_debug_toolbar
- Ensures that social authentication and email settings have been changed and/or configured.
- Uses an email server for transactional emails
When ENV_NAME is prod
# (1)
SECRET_KEY = env("DJANGO_SECRET_KEY")
# (2)
check_auth()
# (3)
EMAIL_BACKEND = "postmark.django_backend.EmailBackend"
# (3)
CSRF_COOKIE_SECURE = True
SECURE_BROWSER_XSS_FILTER = True
X_FRAME_OPTIONS = "DENY"
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 2592000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SECURE_CONTENT_TYPE_NOSNIFF = True
SESSION_COOKIE_SECURE = True
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
- If this
SECRET_KEY
not set, will error out - Ensures that social authentication and email settings have been changed and/or configured.
- Uses an email server for transactional emails
- Refer to Django security settings