In the case of using mod_wsgi with Apache, environment variables (set in the Apache SetEnv directive or otherwise) are passed to the application function in the first argument of environ .
To have access to these environment variables in settings.py (or elsewhere), it is convenient to copy them to os.environ , using, for example,
os.environ['TOP_SECRET'] = environ['TOP_SECRET']
After this environ is passed to django.core.handlers.wsgi.WSGIHandler (via django.core.wsgi.get_wsgi_application ), where it ultimately makes a path to error reporting.
The TOP_SECRET key TOP_SECRET not need to be saved in environ after it is copied to os.environ , therefore changing the line above to os.environ['TOP_SECRET'] = environ.pop('TOP_SECRET', '') removes it from the error reports.
wsgi.py it all together, my wsgi.py file looks like this:
import os from django.core.wsgi import get_wsgi_application os.environ.setdefault("DJANGO_SETTINGS_MODULE", "App.settings") env_variables_to_pass = ['TOP_SECRET'] def application(environ, start_response): # pass the WSGI environment variables on through to os.environ for var in env_variables_to_pass: os.environ[var] = environ.pop(var, '') return get_wsgi_application()(environ, start_response)
This means that the required environment variables are available in os.environ where necessary, but they do not appear in error reports.
It is possible that I missed something, but it seems to work for me. If there is a reason not to do this, post a comment. It may be safer to first create a copy of the environ dictionary, i.e. my_environ = copy.deepcopy(environ) and then use instead of environ .
Note also that other sensitive variables (e.g. passwords in POST requests) must be filtered out .
zelanix
source share