Christoffer
Christoffer

Reputation: 7993

Set DJANGO_SETTINGS_MODULE through uwsgi

I am trying to find the best way to split my django settings, and have the different settings files served for dev and prod.

My wsgi file:

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

and in my uwsgi.ini file I have this:

env = DJANGO_SETTINGS_MODULE=project.settings.local
wsgi-file = project/wsgi.py

But the env from the uwsgi.ini file is not passed to wsgi. If I print os.environ in the wsgi file the DJANGO_SETTINGS_MODULE is not set.

Any ideas?

Upvotes: 2

Views: 2386

Answers (2)

Robert Townley
Robert Townley

Reputation: 3574

I like doing something like this in wsgi.py:

import os
import socket

socket_name = socket.gethostname()
if "stage" in socket_name:
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings.staging')
elif "prod" in socket_name:
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings.production')
else:
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings.local')

application = get_wsgi_application()

Your method of detecting whether you're on staging, prod, or local can be whatever you'd like, but the os.environ.setdefault command seems like it'll accomplish what you're hoping to do.

Upvotes: 5

Christoffer
Christoffer

Reputation: 7993

My solution was to also set the DJANGO_SETTINGS_MODULE in the bin/activate file. I have added it after export PATH like so:

export PATH
export DJANGO_SETTINGS_MODULE='project.settings.local'

You should find your bin folder in your project environment folder.

Upvotes: 1

Related Questions