12

I need to split Django's development and production settings. I decided that if USKOVTASK_PROD variable is set, then app should use production settings. I read this article and tried to do it.

My snippets:

/etc/apache2/sites-enabled/uskovtask.conf:

<VirtualHost *:80>

ServerName uskovtask.*.com
ServerAlias uskovtask.*.com
DocumentRoot /mnt/ebs/uskovtask


Alias /static /mnt/ebs/uskovtask/static/
<Directory /mnt/ebs/uskovtask/static>
    Require all granted
</Directory>

#WSGIPythonPath /mnt/ebs/uskovtask
WSGIDaemonProcess uskovtask.*.com python-path=/mnt/ebs/uskovtask:/usr/lib/python2.7/site-packages
WSGIProcessGroup uskovtask.*.com
WSGIScriptAlias / /mnt/ebs/uskovtask/uskovtask/wsgi.py
SetEnv USKOVTASK_PROD 1


<Directory /mnt/ebs/uskovtask/uskovtask>
<Files wsgi.py>
    Require all granted
</Files>
</Directory>

</VirtualHost>

wsgi.py:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "uskovtask.settings")

from django.core.wsgi import get_wsgi_application

_application = get_wsgi_application()

def application(environ, start_response):
    if 'USKOVTASK_PROD' in environ:
        os.environ.setdefault('USKOVTASK_PROD', environ['USKOVTASK_PROD'])
    return _application(environ, start_response)

settings.py's part:

import os

if 'USKOVTASK_PROD' in os.environ:
    from settings_prod import *
else:
    from settings_dev import *

But it always imports settings_dev's settings. Why?

michaeluskov
  • 1,729
  • 7
  • 27
  • 53
  • Can you debug and verify if USKOVTASK_PROD really sets in os.environ ? You can place a simple print for that. – iamkhush Nov 17 '14 at 19:02
  • @iamkhush USKOVTASK_PROD is in os.enivron if I print it in `wsgi.py`, but not in `settings.py` – michaeluskov Nov 17 '14 at 19:08
  • Can you verify if the statement "if 'USKOVTASK_PROD' in environ" is true by placing print after it. – iamkhush Nov 17 '14 at 19:14
  • @iamkhush it is False – michaeluskov Nov 17 '14 at 19:20
  • Ok, then its clear that the problem is with apache conf file not settings the variable correctly. – iamkhush Nov 17 '14 at 19:34
  • @iamkhush Oh, no, sorry, it is True, but `'USKOVTASK_PROD' in os.environ` in `settings.py` is False – michaeluskov Nov 17 '14 at 19:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65099/discussion-between-iamkhush-and-michaeluskov). – iamkhush Nov 17 '14 at 20:17
  • Sorry. This has nothing to do with the answer. But it is a really bad idea to post the URL to a django site which you have running with DEBUG=True. Please edit your answer and replace your URLs with something like blabla.myproject.com or so. – andzep Nov 17 '14 at 20:37

2 Answers2

11

I solved this problem by changing wsgi.py to this:

from django.core.handlers.wsgi import WSGIHandler
import django
import os

class WSGIEnvironment(WSGIHandler):

    def __call__(self, environ, start_response):

        os.environ['USKOVTASK_PROD'] = environ['USKOVTASK_PROD']
        os.environ.setdefault("DJANGO_SETTINGS_MODULE", "uskovtask.settings")
        django.setup()
        return super(WSGIEnvironment, self).__call__(environ, start_response)

application = WSGIEnvironment()
michaeluskov
  • 1,729
  • 7
  • 27
  • 53
  • 1
    Can confirm that this worked for me with Apache/2.2.22 (Debian), mod_wsgi/4.4.8, Python/3.4.2, Django/1.7.3 (Of course, after changing 'USKOVTASK_PROD' and 'uskovtask.settings' to match my ENV variable and app name.) – Mike Covington Feb 11 '15 at 08:20
  • I was using Django 1.6 and env vars were working fine, then I updated to Django 1.8 and env vars weren't loading at all and I was given default values everywhere... Then I found your answer! I guess this is the route to go for Django > 1.6, thanks! – Emile Bergeron Apr 27 '15 at 16:59
7

This is related to question Access Apache SetEnv variable from Django wsgi.py file

You need to inherit WSGIHandler as the answer says.

As Graham Dumpleton explains in the second answer,

That all said, the blog post you mention will not usually help. This is because it is using the nasty trick of setting the process environment variables on each request based on the per request WSGI environ settings set using SetEnv in Apache. This can cause various issues in a multi threading configuration if the values of the environment variables can differ based on URL context. For the case of Django, it isn't helpful because the Django settings module would normally be imported before any requests had been handled, which means that the environment variables would not be available at the time required.

and I think this is what is happening in your case.

Community
  • 1
  • 1
iamkhush
  • 2,562
  • 3
  • 20
  • 34