views:

369

answers:

2

I have this line in my wsgi.conf file:

WSGIScriptAlias /trunk "c:/app/trunk/app.wsgi"

Inside of my django settings file, I need to know the alias "/trunk" to get LOGIN_URL to work properly. How can I retrieve this value from my apache settings?

Thanks! Pete

A: 

Since you want to obtain a value from the Apache configuration, I guess the only thing you can do is read the file and process it.

Something like (assuming your settings.py lives in the same directory as wsgi.conf):

try:
    f = open('wsgi.conf', 'r')
    LOGIN_URL=[line for line in f.readlines() if 'WSGIScriptAlias' in line][0].split()[1]
finally:
    f.close()

Catching an exception if the file is not there might be a good idea too.


Edit after your comment: Ah, I see what you are trying to do. This thread may be helpful in understanding why using os.environ won't work. The alternative they present won't help you though:

In a nutshell, the apache SetEnv isn't setting values in the process environment that os.environ represents. Instead SetEnv is setting values in the context of the WSGI request.

Within your code you can reference that context at request.environ:

def myView(request): 
    tier = request.environ['TIER'] 

It's me again. Because of what Apache's SetEnv is doing, I don't think you will have access to the variable in settings.py. It seems parsing the file is still the only option.

Further Edit: Another alternative - can you base your decision on the host name?

 #settings.py
 import socket

 production_servers = { 'server1.com': '/trunk...',
                        'server2.com': '/trunk_2...' }

 LOGIN_URL=production_servers[ socket.gethostname() ]    

This completely sidesteps the information contained in apache configuration.

celopes
There's no way to get it from the apache environment? Can I possibly declare it as a variable in the apache config?
slypete
@slypete: The Apache config, as far as I know, has nothing to do with the python or the Django environment. This wsgi.conf file is included by the httpd.conf and read by the Apache server. Up to that point, python does not come into play. When a request is made to the url defined in wsgi.conf, python comes into play to serve your django site. I don't see how you would declare a python variable in the apache config. At least, that is my understanding.
celopes
My thoughts are to set a apache environment variable, and then pick it up from python using os.environ. It doesn't seem to be working, however.
slypete
@slypete: see my edit.
celopes
@slypete: thought of another possible solution, see edit.
celopes
@celopes, thanks for your efforts, but I'm not satisfied with any of the solutions provided here.
slypete
@slypete: No problem. When you find an alternative that works for you, post it here. I'm sure curious and certain others will be too. Maybe Graham is right and you should bring it up with the Django luminaries.
celopes
+2  A: 

Access the original WSGI environ dictionary for a specific request and lookup the 'SCRIPT_NAME' variable. The value of this is the notional mount point for the WSGI application as specified when using WSGIScriptAlias. Getting it through the per request environment is the only real way of doing it automatically. You cannot get to it from outside of a request and there should be no real need for you to do that.

By rights, Django should provide a way of automatically having the mount point of the application inserted into configured URLs such as that. You should perhaps bring up the issue on the official Django users list instead if you cannot find the way of doing it as perhaps a change in Django is needed.

Graham Dumpleton