settings.DEBUG 的值在 Django Test 中的设置和 url 之间变化

Pie*_*las 1 python testing django

我正在尝试为一些仅在调试中设置的 URL 设置测试。它们没有设置,因为显然我的设置文件和 urls.py 之间的 DEBUG 值更改为 False。我以前从未遇到过这个问题,而且我不记得做过任何涉及 DEBUG 值的特别花哨的事情。

这是我的 urls.py :

from django.conf import settings
from my_views import dnfp
print "settings.DEBUG in url: {}".format(settings.DEBUG)
if settings.DEBUG:
    urlpatterns += [url(r'^dnfp/$', dnfp, name="debug_not_found_page"...
Run Code Online (Sandbox Code Playgroud)

这是我的设置文件:

DEBUG=True
print "DEBUG at the end of the settings: {}".format(DEBUG)
Run Code Online (Sandbox Code Playgroud)

在我的测试中失败的内容:

 reverse("debug_not_found_page"), 
Run Code Online (Sandbox Code Playgroud)

这是测试的输出:

DEBUG at the end of the settings: True
settings.DEBUG in url: False
Creating test database for alias 'default'...
.E
(...)
NoReverseMatch: Reverse for 'debug_not_found_page' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Run Code Online (Sandbox Code Playgroud)

如果我自己更改 urls.py 中的值,则会再次设置 url,并且测试将使用此 urls.py :

from django.conf import settings
from my_views import dnfp
settings.DEBUG = True
if settings.DEBUG:
    urlpatterns += [url(r'^dnfp/$', dnfp, name="debug_not_found_page"...
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?我的 DEBUG 值何时以及为何在设置和 url 之间发生变化?

vis*_*ell 6

来自文档

无论配置文件中 DEBUG 设置的值如何,所有 Django 测试都以 DEBUG=False 运行。这是为了确保观察到的代码输出与生产环境中看到的输出相匹配。

  • 多谢 !对于那些如何通过谷歌搜索的人,下一步就在这里:http://stackoverflow.com/questions/7447134/how-do-you-set-debug-to-true-when-running-a-django-test (2认同)