title: Passing environment variables from apache via mod_wsgi to use in django 1.11 settings
body: <p>Found a few versions of this question, such as <a href="https://stackoverflow.com/questions/20102620/django-get-environment-variables-from-apache">Django get environment variables from apache</a>, however the advice I've found so far doesn't seem to work with the latest LTS django (1.11).</p>
  
  <p>I have an apache configuration which holds a number of environment variables, not limited to connection credentials for the DB. Im using this to make my code portable between dev/prod etc.</p>
  
  <p>My apache conf just uses SetEnv to pass in some variables.</p>
  
  <p>I've tried two different styles of approach to use these variables, both seem to suffer from the same issue; it needs to read the settings file before we can write to the environment, and the settings file requires values from the environment.</p>
  
  <p>My two variants are;</p>
  
  <pre><code>import os
  import django
  from django.core.handlers.wsgi import WSGIHandler
  
  from django.core.wsgi import get_wsgi_application
  
  _application = get_wsgi_application()
  
  def application(environ, start_response):
    for key in [keys...]:
      if environ.get(key):
          os.environ[key] = environ.get(key)
    return _application(environ, start_response)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>import os
  import django
  from django.core.handlers.wsgi import WSGIHandler
  
  class WSGIEnvironment(WSGIHandler):
      def __call__(self, environ, start_response):
          for key in [keys...]:
              if environ.has_key(key):
                  print "Key: %s = %s" % (key,environ[key])
                  os.environ[key] = environ[key]
          return super(WSGIEnvironment, self).__call__(environ, start_response)
  
  os.environ.setdefault("DJANGO_SETTINGS_MODULE", 'apiClient.settings')
  django.setup(set_prefix=False)
  application = WSGIEnvironment()
  </code></pre>
  
  <p>Either way im trying to use the values in settings as;</p>
  
  <pre><code>KEY = "thing"
  
  if os.environ.has_key("KEY"):
      KEY = os.environ["KEY"]
  </code></pre>