Differential settings for production vs development?

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?

OK. Our sysadmin was able to set an os-level environment variable and that seems to have had the desired effect. Question is: is that a good strategy?

Yep, this is perfectly reasonable, I have done this in a number of projects. Although I would possibly recommend the default being development and not production.

To be clear the os.environ in python is reading from system environment variables that need to set outside of the application. the setdefault function set’s a default if there isn’t a variable set.

My concern is that if we had multiple virtual hosts with different settings, this solution wouldn’t work. It works for us RN, because we have only 1 virtual host on that machine, but in terms of our documentation for external folks who install our software, it could be not workable.

This is likely more in depth conversation to have with your system admin team, but environment variables are fair standard for setting up web apps, as an example search the 12 factor app model. (ie I would expect any secrets to likely have some reference in an environment variable)

To your specific point on virtual hosts, IIRC you ought to be able to run each virtual host as a different user which can set environment variables on a per user basis, they can also be set per login or per session as well as globally.

Generally it’s hard to say more to your specific scenario without more context, only that it is a workable strategy to have multiple settings files for each environment