如何从 django 设置文件访问 WSGIScriptAlias?
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
访问特定请求的原始 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.
由于您想从 Apache 配置中获取值,我想您唯一能做的就是读取文件并处理它。
类似于(假设您的settings.py与wsgi.conf位于同一目录中):
如果文件不存在则捕获异常也可能是一个好主意。
评论后编辑:啊,我明白你想做什么。 此帖子可能有助于理解为什么使用
os.environ
将不起作用。他们提供的替代方案对您没有帮助:又是我。由于 Apache 的 SetEnv 所做的事情,我认为您无法访问 settings.py 中的变量。看来解析文件仍然是唯一的选择。
进一步编辑:另一种选择 - 您可以根据主机名做出决定吗?
这完全回避了 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):
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: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?
This completely sidesteps the information contained in apache configuration.