如何从 django 设置文件访问 WSGIScriptAlias?

发布于 2024-08-19 06:34:40 字数 197 浏览 5 评论 0原文

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

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

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

谢谢! 皮特

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弥繁 2024-08-26 06:34:40

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

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

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.

奶茶白久 2024-08-26 06:34:40

由于您想从 Apache 配置中获取值,我想您唯一能做的就是读取文件并处理它。

类似于(假设您的settings.py与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()

如果文件不存在则捕获异常也可能是一个好主意。


评论后编辑:啊,我明白你想做什么。 此帖子可能有助于理解为什么使用 os.environ 将不起作用。他们提供的替代方案对您没有帮助:

简而言之,apache SetEnv 没有在进程中设置值
os.environ 代表的环境。相反 SetEnv 正在设置
WSGI 请求上下文中的值。

在您的代码中,您可以在 request.environ 处引用该上下文:

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

又是我。由于 Apache 的 SetEnv 所做的事情,我认为您无法访问 settings.py 中的变量。看来解析文件仍然是唯一的选择。

进一步编辑:另一种选择 - 您可以根据主机名做出决定吗?

 #settings.py
 import socket

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

 LOGIN_URL=production_servers[ socket.gethostname() ]    

这完全回避了 apache 配置中包含的信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文