I have multiple settings files in my Django project (base.py, local.py, production.py).
Locally, I'm kicking off the Django dev server via
python manage.py runserver --settings=config.settings.local
How can I tell mod_wsgi to use the same file? I'm testing this on my local apache instance, but will need to figure out the same thing for my production apache instance.
--settings=config.settings.production
Current httpd-vhosts.conf:
WSGIDaemonProcess secureDash python-path=/Users/user/projects/secureDash_project/secureDash python-home=/Users/user/.venvs/securedash_py3.6
WSGIProcessGroup secureDash
WSGIScriptAlias / /Users/user/projects/secureDash_project/config/wsgi.py
Error that I see in the apache log:
ModuleNotFoundError: No module named 'secureDash.settings'
Django 1.11
mod_wsgi-4.5.15
Apache 2.4.18
You do this by setting the DJANGO_SETTINGS_MODULE environment variable. You can either do this in your Apache vhost configuration, or in the wsgi file itself.
I solved this by adding DJANGO_SETTINGS_MODULE to my secrets.json file in each environment.
secrets.json:
{
"FILENAME": "secrets.json",
"SECRET_KEY": "someSuperSecretiveSecret!",
"DJANGO_SETTINGS_MODULE": "config.settings.local"
}
wsgi.py:
import json
from django.core.exceptions import ImproperlyConfigured
from pathlib import Path
...
# Directory Paths
BASE_DIR = Path(__file__).resolve().parent
# JSON-based secrets module
SECRETS_JSON = str(BASE_DIR) + '/secrets.json'
with open(SECRETS_JSON) as f:
secrets = json.loads(f.read())
def get_secret(setting, secrets=secrets):
'''Get the secret variable or return explicit exception'''
try:
return secrets[setting]
except KeyError:
error_msg = 'Set the {0} environment variable'.format(setting)
raise ImproperlyConfigured(error_msg)
DJANGO_SETTINGS_MODULE = get_secret('DJANGO_SETTINGS_MODULE')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", DJANGO_SETTINGS_MODULE)