We are trying to split our Django settings file into base.py, dev.py, and prod.py. It works, except our development server is using the production settings. Before the split, we controlled the various debug settings in the .env file, but after the split, we don’t use the .env file for the debug settings. They are hard-coded in project/settings/dev.py, which is what we want, but I cannot figure out how to tell the development server to use project.settings.dev instead of the default (prod) using os.environ.setdefault().
Also, during this transition, I want other branches to still work without like, having to muck with the environment. So here’s what we have so far:
TraceBase/wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "TraceBase.settings.prod")
print(f"USING {os.environ.get('DJANGO_SETTINGS_MODULE')} SETTINGS 2")
application = get_wsgi_application()
TraceBase/settings/base.py has all the common settings…
TraceBase/settings/__init__.py
from .dev import *
# The above is a backward-compatibility import. This makes it possible for old
# branches to still specify `TraceBase.settings` in their wsgi.py file and still
# work, without having to set DJANGO_SETTINGS_MODULE in the apache config when
# checking out different branches.
TraceBase/settings/dev.py
import sys
from .base import *
print("USING DEV SETTINGS")
...
TraceBase/settings/prod.py
from .base import *
print("USING PROD SETTINGS")
...
I have tried various things, like using the SetEnv directive in the apache config, but it doesn’t work. The wsgi.py’s os.environ never sees the setting. The log always shows multiple settings files being run, and the last one is the wsgi.py one that is not getting the os environment setting. E.g.:
[Wed May 13 15:11:37.101414 2026] [wsgi:error] [pid 3097548:tid 3097652] USING TraceBase.settings.prod SETTINGS 2
[Wed May 13 15:11:37.114185 2026] [wsgi:error] [pid 3097548:tid 3097652] USING DEV SETTINGS
[Wed May 13 15:11:37.114564 2026] [wsgi:error] [pid 3097548:tid 3097652] USING PROD SETTINGS
Do people just not set up development servers and just use sandboxes? I’m no apache admin and I’m kind of stuck. I’d like the backward compatibility to co-exist with the differential settings, but how do you do that and get the security benefit of not setting DEBUG from the .env files?