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 t开发者_如何学Che alias "/trunk" to get LOGIN_URL to work properly. How can I retrieve this value from my apache settings?
Thanks! Pete
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.
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.
精彩评论