ksrini
ksrini

Reputation: 1642

Accessing request header in django mod_wsgi script

I have a django app running on apache. I use mod_wsgi. I'm looking to set a few env variables in the wsgi.py script before creating the 'application'. But I want to set the env variables based on certain properties in the request header.

I can't set the env variables using a django middleware because the env variables are needed at the time of loading some of the python modules which seems to happen before the middleware's process_requestfunction is invoked.

So my question is - How/where can I initialize the env variables based on the request header before the 'application' is created in my wsgi script?

This is how I'm creating the application in wsgi.py -

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

The import of django.core.handlers.wsgi is what is triggering loading of my application's python modules that need the env variables to be defined while loading.

Thanks,

Upvotes: 0

Views: 918

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

You can't and shouldn't. The WSGI process is persistent across multiple requests, you don't initialize it from new each time.

If your middleware depends on certain elements in the request, you should check those explicitly there, rather than relying on environment variables.

Upvotes: 0

Related Questions