我如何知道我的Django应用程序是否在开发服务器上运行? [英] How can I tell whether my Django application is running on development server or not?

查看:524
本文介绍了我如何知道我的Django应用程序是否在开发服务器上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定我的应用程序是否在开发服务器上运行?我想我可以检查 settings.DEBUG 的值,假设如果 DEBUG True 然后它在开发服务器上运行,但我宁可知道,而不是依靠约定。

How can I be certain that my application is running on development server or not? I suppose I could check value of settings.DEBUG and assume if DEBUG is True then it's running on development server, but I'd prefer to know for sure than relying on convention.

推荐答案

server = request.META.get('wsgi.file_wrapper', None)
if server is not None and server.__module__ == 'django.core.servers.basehttp':
    print('inside dev')

当然,可以在META上设置wsgi.file_wrapper ,并在另一个服务器上通过非常巧合从名为 django.core.servers.basehttp 的模块中获取类环境,但我希望这将覆盖你。

Of course, wsgi.file_wrapper might be set on META, and have a class from a module named django.core.servers.basehttp by extreme coincidence on another server environment, but I hope this will have you covered.

顺便说一句,我发现这是通过在开发服务器上运行时合成无效的模板,并搜索有趣的追溯请求信息部分的内容,所以我只是编辑我的回答Nate的想法。

By the way, I discovered this by making a syntatically invalid template while running on the development server, and searched for interesting stuff on the Traceback and the Request information sections, so I'm just editing my answer to corroborate with Nate's ideas.

这篇关于我如何知道我的Django应用程序是否在开发服务器上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆