确定Django是否正在开发服务器下运行 [英] Determine if Django is running under the development server

查看:124
本文介绍了确定Django是否正在开发服务器下运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法确定Django是否在localhost上运行,并在 settings.py 中设置 DEBUG 变量因此,如果我在本地运行服务器,将会将 DEBUG 设置为 True ,否则设置为 False



Localhost: python manage.py runserver

不是localhost: python manage.py runserver 0.0.0.0:8000

解决方案

根据Bernhard Vallant的建议,您可以在<$ c $中检查 runserver c> sys.argv 。



您可以将 DEBUG code> settings.py 与此:

  DEBUG =(sys.argv [1 ] =='runserver')

您还应该 import sys settings.py 中的某个地方。


Is there a way to determine if Django is running on localhost and setting the DEBUG variable in settings.py accordingly.

So that if I run the server locally it will set DEBUG to True and otherwise set it to False.

Localhost: python manage.py runserver
Not localhost: python manage.py runserver 0.0.0.0:8000

解决方案

As suggested by Bernhard Vallant, you can just check for runserver in sys.argv.

You can just replace your DEBUG assignment in settings.py with this:

DEBUG = (sys.argv[1] == 'runserver')

You should also import sys somewhere in settings.py.

这篇关于确定Django是否正在开发服务器下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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