【问题标题】:Django 4: settings.DEBUG not correct when running testsDjango 4:运行测试时,settings.DEBUG 不正确
【发布时间】:2022-09-24 07:03:41
【问题描述】:

我正在尝试编写 Django (4) 测试。它必须根据我的 settings.py 中的 DEBUG 常量以不同的方式工作。 所以我从以下内容开始:

def my_test(self):
    from django.conf import settings

    if settings.DEBUG:
    ....
    else:
    ....

我注意到代码从未执行过 if 的第一部分,所以我添加了:

print(settings.DEBUG)

我注意到这个打印总是错误的。即使在我的 settings.py 中将 DEBUG 设置为 True。

我在那里尝试了其他一些常量(INSTALLED_APPS,ALLOWED_HOSTS),它们都返回了正确的值。

这是为什么?如何在我的 settings.py 中访问真正的 DEBUG 常量?

    标签: django-4.0


    【解决方案1】:

    添加--debug-mode 标志解决了这个问题:

    python manage.py test --debug-mode

    --debug-mode
    
    Sets the DEBUG setting to True prior to running tests. This may help troubleshoot test failures.
    

    【讨论】:

      猜你喜欢
      • 2020-02-28
      • 1970-01-01
      • 2011-05-04
      • 2016-06-20
      • 2020-03-09
      • 1970-01-01
      • 2022-11-11
      • 2011-01-03
      • 1970-01-01
      相关资源
      最近更新 更多