确定Django是否正在开发服务器下运行 [英] Determine if Django is running under the development server
问题描述
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屋!