0

我的 wsgi.conf 文件中有这一行:

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

在我的 django 设置文件中,我需要知道别名“/trunk”才能使 LOGIN_URL 正常工作。如何从我的 apache 设置中检索此值?

谢谢!皮特

4

2 回答 2

2

访问特定请求的原始 WSGI 环境字典并查找“SCRIPT_NAME”变量。this 的值是使用 WSGIScriptAlias 时指定的 WSGI 应用程序的名义挂载点。通过每个请求环境获取它是自动执行此操作的唯一真正方法。您无法从请求之外访问它,并且您应该没有真正需要这样做。

按理说,Django 应该提供一种自动将应用程序的挂载点插入到配置的 URL 中的方法,例如。如果您找不到这样做的方法,您也许应该在官方 Django 用户列表中提出这个问题,因为可能需要对 Django 进行更改。

于 2010-01-31T22:08:27.207 回答
0

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.

于 2010-01-28T21:37:14.650 回答