Django:从manage.py runserver命令覆盖Debug = True

时间:2022-06-12 23:18:49

Is there an easy way to tell Django's runserver to override a single variable in the settings.py file?

有没有一种简单的方法可以告诉Django的runserver覆盖settings.py文件中的单个变量?

I would love to be able to call:

我很乐意打电话给:

python manage.py runserver 0.0.0.0:8000 Debug=False

Any thoughts?

有什么想法吗?

Motive: have a particular site where there are hundreds of database queries to show or save a particular page, I'd like to be able to turn off debug quickly without having to edit my settings file (which has the possibility of being forgotten).

动机:有一个特定的网站,有数百个数据库查询来显示或保存特定的页面,我希望能够快速关闭调试,而无需编辑我的设置文件(有可能被遗忘)。

2 个解决方案

#1


15  

I think you have two options

我想你有两个选择

The simplest is probably a custom settings override, something like:

最简单的可能是自定义设置覆盖,类似于:

# no_debug_settings.py

# pull in the normal settings
from settings import *

# no debug for us
DEBUG = False

Now, when you want to start without debugging, you'd run:

现在,当你想在没有调试的情况下启动时,你将运行:

python manage.py runserver --settings=no_debug_settings 0.0.0.0:8000 

As an alternative, you could just customise your manage.py file. That imports settings, and passes it to the execute_manager. If you added some code between the import and the call, you could have it check for extra arguments and alter the settings as needed. It's a bit more fiddly and prone to break / be forgotten, so I'd suggest the override settings wrapper is probably your best way to go.

或者,您可以自定义manage.py文件。导入设置,并将其传递给execute_manager。如果您在导入和调用之间添加了一些代码,则可以检查是否有额外的参数并根据需要更改设置。它更加繁琐,容易被打破/被遗忘,所以我建议覆盖设置包装器可能是你最好的方法。

#2


5  

I edited my settings.py file with a conditional block, like this:

我用条件块编辑了我的settings.py文件,如下所示:

import os  # If needed.

if os.environ.get('DJANGO_DEBUG'):
    print("Debug is enabled.")
    DEBUG = True
    # When not specified, ALLOW_HOSTS defaults to:
    # ALLOWED_HOSTS = ['localhost', '127.0.0.1', '[::1]']
else:
    DEBUG = False
    ALLOWED_HOSTS = ["*"]

Then, run your server by passing the environmental variable DJANGO_DEBUG=1. You can name the variable anything you want so long as you are consistent:

然后,通过传递环境变量DJANGO_DEBUG = 1来运行您的服务器。只要您保持一致,就可以将变量命名为任何名称:

DJANGO_DEBUG=1 python -Wall manage.py runserver

Omit that environmental variable when calling manage.py to disable debug (because setting it to any value, including 0 will still make it true to the Python code.)

在调用manage.py来禁用调试时省略该环境变量(因为将其设置为任何值,包括0仍将使其适用于Python代码。)

Update: A commenter stated that the ALLOWED_HOSTS directive is ignored when DEBUG is True. This is only true in older versions of Django. The current behavior is to honor ALLOWED_HOSTS or default to localhost addresses if it isn't specified when DEBUG is enabled. My answer is updated to reflect this as a minor correction.

更新:一位评论者表示,当DEBUG为True时,将忽略ALLOWED_HOSTS指令。这仅适用于旧版本的Django。当启用DEBUG时未指定时,当前行为是为了尊重ALLOWED_HOSTS或默认为localhost地址。我的答案已更新,以反映这是一个小的修正。

This is sourced from the Django documentation:

这来自Django文档:

When DEBUG is True and ALLOWED_HOSTS is empty, the host is validated against ['localhost', '127.0.0.1', '[::1]']

当DEBUG为True且ALLOWED_HOSTS为空时,主机将针对['localhost','127.0.0.1','[:: 1]']进行验证

Additionally, it states that the behavior your comment on is now outdated in a few major version lines:

此外,它表明您的评论行为现在已在几个主要版本行中过时:

In older versions, ALLOWED_HOSTS wasn’t checked if DEBUG=True. This was also changed in Django 1.10.3, 1.9.11, and 1.8.16 to prevent a DNS rebinding attack.

在旧版本中,如果DEBUG = True,则不检查ALLOWED_HOSTS。这也在Django 1.10.3,1.9.11和1.8.16中发生了变化,以防止DNS重新绑定攻击。

#1


15  

I think you have two options

我想你有两个选择

The simplest is probably a custom settings override, something like:

最简单的可能是自定义设置覆盖,类似于:

# no_debug_settings.py

# pull in the normal settings
from settings import *

# no debug for us
DEBUG = False

Now, when you want to start without debugging, you'd run:

现在,当你想在没有调试的情况下启动时,你将运行:

python manage.py runserver --settings=no_debug_settings 0.0.0.0:8000 

As an alternative, you could just customise your manage.py file. That imports settings, and passes it to the execute_manager. If you added some code between the import and the call, you could have it check for extra arguments and alter the settings as needed. It's a bit more fiddly and prone to break / be forgotten, so I'd suggest the override settings wrapper is probably your best way to go.

或者,您可以自定义manage.py文件。导入设置,并将其传递给execute_manager。如果您在导入和调用之间添加了一些代码,则可以检查是否有额外的参数并根据需要更改设置。它更加繁琐,容易被打破/被遗忘,所以我建议覆盖设置包装器可能是你最好的方法。

#2


5  

I edited my settings.py file with a conditional block, like this:

我用条件块编辑了我的settings.py文件,如下所示:

import os  # If needed.

if os.environ.get('DJANGO_DEBUG'):
    print("Debug is enabled.")
    DEBUG = True
    # When not specified, ALLOW_HOSTS defaults to:
    # ALLOWED_HOSTS = ['localhost', '127.0.0.1', '[::1]']
else:
    DEBUG = False
    ALLOWED_HOSTS = ["*"]

Then, run your server by passing the environmental variable DJANGO_DEBUG=1. You can name the variable anything you want so long as you are consistent:

然后,通过传递环境变量DJANGO_DEBUG = 1来运行您的服务器。只要您保持一致,就可以将变量命名为任何名称:

DJANGO_DEBUG=1 python -Wall manage.py runserver

Omit that environmental variable when calling manage.py to disable debug (because setting it to any value, including 0 will still make it true to the Python code.)

在调用manage.py来禁用调试时省略该环境变量(因为将其设置为任何值,包括0仍将使其适用于Python代码。)

Update: A commenter stated that the ALLOWED_HOSTS directive is ignored when DEBUG is True. This is only true in older versions of Django. The current behavior is to honor ALLOWED_HOSTS or default to localhost addresses if it isn't specified when DEBUG is enabled. My answer is updated to reflect this as a minor correction.

更新:一位评论者表示,当DEBUG为True时,将忽略ALLOWED_HOSTS指令。这仅适用于旧版本的Django。当启用DEBUG时未指定时,当前行为是为了尊重ALLOWED_HOSTS或默认为localhost地址。我的答案已更新,以反映这是一个小的修正。

This is sourced from the Django documentation:

这来自Django文档:

When DEBUG is True and ALLOWED_HOSTS is empty, the host is validated against ['localhost', '127.0.0.1', '[::1]']

当DEBUG为True且ALLOWED_HOSTS为空时,主机将针对['localhost','127.0.0.1','[:: 1]']进行验证

Additionally, it states that the behavior your comment on is now outdated in a few major version lines:

此外,它表明您的评论行为现在已在几个主要版本行中过时:

In older versions, ALLOWED_HOSTS wasn’t checked if DEBUG=True. This was also changed in Django 1.10.3, 1.9.11, and 1.8.16 to prevent a DNS rebinding attack.

在旧版本中,如果DEBUG = True,则不检查ALLOWED_HOSTS。这也在Django 1.10.3,1.9.11和1.8.16中发生了变化,以防止DNS重新绑定攻击。