如何在生产服务器上非侵入式地触发 django DEBUG
我正在寻找一种安全的方法来触发 django 生产服务器上的 INTERNAL_IPS 请求的 DEBUG,而不需要更改 settings.py 文件。主要是为了让一些设计人员可以使用工具栏检查实时数据/媒体上的问题,但不依赖他们在完成后重置设置。
与此方法类似。然而这仅适合部署。
过去在基于 php 的系统上我有 mydomain.com 和一个演示 mydomaincom.myprodserver.com 其中 prodserver域可以自动运行基于 $_SERVER['HOST_NAME'] 的调试代码,但 django 缺乏简单的超级全局。例如,在博客示例中,主机名是 /etc/hostname 而不是虚拟主机。
任何想法表示赞赏。
编辑:
我有一种解决方案(但理想情况下我更喜欢更便携的解决方案),方法是将 /path/to/django_in_debug/ 添加到 mydomaincom.myprodserver.com vhost 条目的 sys.path 中。然后在settings.py文件中
try:
from django_in_debug.settings import *
except:
DEBUG = False
I'm looking for a safe method of triggering DEBUG for INTERNAL_IPS requests on a django production server without requiring the alteration of a settings.py file. Mainly to get the toolbar going for some designers to check issues on live data/media, but without relying on them to reset the settings once they have finished.
Similar to this method. hovever this only suits deployment.
in the past on php based systems I've had mydomain.com and a demo mydomaincom.myprodserver.com where the prodserver domain can automatically run debug code based on $_SERVER['HOST_NAME'] but django is lacking the easy superglobal. eg in the blog example hostname is the /etc/hostname not the vhost.
Any ideas appreciated.
Edit:
I have a workaround solution of sorts (but ideally I'd prefer a more portable one) by adding a /path/to/django_in_debug/ to the sys.path of the mydomaincom.myprodserver.com vhost entry. Then in the settings.py file
try:
from django_in_debug.settings import *
except:
DEBUG = False
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要求做的事情比看起来要复杂一些。您想要显示请求级别发生的某些 INTERNAL_IPS 的调试信息。但是,您正在谈论位于站点级别的settings.py。
为了实现这一目标,您必须根据每个请求重新评估 settings.py,正如您所知,这可能是一个非常糟糕的方向。根据 Django 自己的文档,在加载网站后修改其设置是不允许的(公平地说,人们可以这样做,但根据 Django 的官方立场,这毫无价值)。
这是给您的一个想法:
您有 2 个 WSGI 文件。第一个 WSGI 文件指向您的主 settings.py,apache 将流量从 www.yourdomain.com 定向到它。第二个 WSGI 文件指向 debug_settings.py,apache 将流量从 debug.yourdomain.com 重定向到它。 debug_settingsg.py 看起来像这样:
从这里编写一个简单的中间件组件来捕获传入的请求。将请求 IP 与 settings.INTERNAL_IPS 进行比较,如果发现匹配,则请求将重定向到 debug.yourdomain.com。
这允许您保留站点的 1 个副本,但根据请求级别值更改站点级别设置。
What you're asking to do is a bit more complex than it seems. You want to show debug information for certain INTERNAL_IPS which is happening at the request-level. However, you're talking about settings.py which is at the site-level.
To achieve this then, you would have to have the settings.py being re-evaluated per each request, which as you can tell is probably a very bad direction. Per Django's own documentation, modifying the settings of the site after it has loaded is a no-no (to be fair people get away with this, but it's worth nothing Django's official stance).
Here's an idea for you:
You have 2 WSGI files. The first WSGI file points to your main settings.py, and apache directs traffic from www.yourdomain.com to it.. The second WSGI file points to debug_settings.py, and apache redirects traffic from debug.yourdomain.com to it. debug_settinsg.py looks like this:
From here you write a simple middleware component to trap incoming requests. The request IP is compared to the settings.INTERNAL_IPS and if a match is found the request is redirected to debug.yourdomain.com.
This allows you to keep 1 copy of the site, but change a site-level setting based on a request-level value.