1

I use django 1.11 and this is my wsgi.py file

# -*- coding: utf-8 -*-
import os
import sys
from django.core.wsgi import get_wsgi_application

PATH = os.path.abspath(
    os.path.join(os.path.dirname(os.path.abspath(__file__))))

if PATH not in sys.path:
    sys.path.append(PATH)

os.environ['DJANGO_SETTINGS_MODULE'] = 'cms.settings.qa'

ENV_VARIABLES_TO_PASS = ['VAR_1', 'VAR_2']

_application = get_wsgi_application()

def application(environ, start_response):
    # pass the WSGI environment variables on thzrough to os.environ
    for var in ENV_VARIABLES_TO_PASS:
        os.environ[var] = environ.get(var, '1')
    return _application(environ, start_response)

and when i try to get the env on settings file its allways return None.

os.getenv('VAR_1') = None & os.getenv('VAR_2') = None

why is that happend? thanks a lot.

  • https://stackoverflow.com/a/43711849/1073606 could help you – Thomas Schwärzl Apr 09 '18 at 07:22
  • not really help me :\ If i set the variable outside application function, its work, but what I set inside the function is just not working..... :\ – Yossi Shaish Apr 09 '18 at 07:40
  • oh wait...you're trying to write `os.environ` with the value of `environ.get`!? This can't work :D – Thomas Schwärzl Apr 09 '18 at 07:43
  • Are you adding ``SetEnv`` directives to Apache for ``VAR_1`` and ``VAR_2``? If using mod_wsgi, that or using mod_rewrite or mod_headers in fancy ways is the only way to pass ``environ`` variables in a request to the WSGI application. – Graham Dumpleton Apr 09 '18 at 07:57
  • @Thomas Schwärzl - but i have a default. if there is nothing in there its suppose to be '1' – Yossi Shaish Apr 09 '18 at 08:02
  • @GrahamDumpleton - yes I do. – Yossi Shaish Apr 09 '18 at 08:03
  • If you are trying to use ``os.getenv('VAR_1')`` in Django settings file that will not work as the settings file is loaded before the request handler is actually called for the first request. – Graham Dumpleton Apr 09 '18 at 10:12
  • If you want to set host specific environment variables, create a config file, or Python file, as some local on the system, not in your project, and read it, incorporating anything in the environment, at the start of ``wsgi.py`` before importing any Django modules. – Graham Dumpleton Apr 09 '18 at 10:14

0 Answers0