Skip to content

Commit 42fd0c5

Browse files
authored
Enable Django DEBUG in fewer scenarios (#251)
Previously `DEBUG` would be enabled in any environment that wasn't a Heroku app, including an OCI image built using the Python CNB. Instead we would rather such an image act closer to how the app will on Heroku. (We also want as few Heroku-specific conditionals as possible.) GUS-W-17623845.
1 parent e0a409b commit 42fd0c5

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
# running on Heroku - to set env vars for those, see:
44
# https://devcenter.heroku.com/articles/config-vars
55

6-
# This is used in gunicorn.conf.py to set appropriate settings for development vs production.
6+
# This is used by gunicorn.conf.py and Django's settings.py to set appropriate
7+
# configuration for development vs production.
78
ENVIRONMENT="development"
89

910
# An example env var used in the tutorial.

gettingstarted/settings.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,27 @@
3131
# Make sure to use a long unique value, like you would for a password. See:
3232
# https://docs.djangoproject.com/en/5.1/ref/settings/#std-setting-SECRET_KEY
3333
# https://devcenter.heroku.com/articles/config-vars
34-
# SECURITY WARNING: keep the secret key used in production secret!
34+
# SECURITY WARNING: Keep the secret key used in production secret!
3535
SECRET_KEY = os.environ.get(
3636
"DJANGO_SECRET_KEY",
3737
default=secrets.token_urlsafe(nbytes=64),
3838
)
3939

40+
# Django has a debug mode which shows more detailed error messages and also means static assets
41+
# can be served without having to run the production `collectstatic` command. However, this
42+
# debug mode *must only be enabled in development* for security and performance reasons:
43+
# https://docs.djangoproject.com/en/5.1/ref/settings/#std-setting-DEBUG
44+
# Debug mode will be automatically enabled when the project is run via `heroku local` (which
45+
# loads the environment variables set in the `.env` file, where `ENVIRONMENT=development`).
46+
# SECURITY WARNING: Don't run with debug turned on in production!
47+
DEBUG = os.environ.get("ENVIRONMENT") == "development"
48+
4049
# The `DYNO` env var is set on Heroku CI, but it's not a real Heroku app, so we have to
4150
# also explicitly exclude CI:
4251
# https://devcenter.heroku.com/articles/heroku-ci#immutable-environment-variables
4352
IS_HEROKU_APP = "DYNO" in os.environ and not "CI" in os.environ
4453

45-
# SECURITY WARNING: don't run with debug turned on in production!
46-
if not IS_HEROKU_APP:
47-
DEBUG = True
48-
49-
# On Heroku, it's safe to use a wildcard for `ALLOWED_HOSTS``, since the Heroku router performs
54+
# On Heroku, it's safe to use a wildcard for `ALLOWED_HOSTS`, since the Heroku router performs
5055
# validation of the Host header in the incoming HTTP request. On other platforms you may need to
5156
# list the expected hostnames explicitly in production to prevent HTTP Host header attacks. See:
5257
# https://docs.djangoproject.com/en/5.1/ref/settings/#std-setting-ALLOWED_HOSTS

0 commit comments

Comments
 (0)