简体   繁体   English

在Django Test中settings.DEBUG在设置和url之间切换的值

[英]Value of settings.DEBUG changing between settings and url in Django Test

I'm trying to set up test for some URLS that are set only in debug. 我正在尝试为仅在调试中设置的某些URL设置测试。 They are not set because apparently the value of DEBUG change to False between my setting file and urls.py. 它们未设置,因为显然我的设置文件和urls.py之间的DEBUG值变为False。 I've never encountered this problem before, and I don't remember doing anything particularly fancy involving DEBUG value. 我之前从未遇到过这个问题,我不记得做任何涉及DEBUG价值的特别花哨的东西。

Here's my urls.py : 这是我的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"...

Here's my setting file : 这是我的设置文件:

DEBUG=True
print "DEBUG at the end of the settings: {}".format(DEBUG)

The content that fail in my test : 我测试中失败的内容:

 reverse("debug_not_found_page"), 

Here's the output of the test : 这是测试的输出:

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: []

If I change the value myself in urls.py the url is set again and the test works with this urls.py : 如果我在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"...

Any ideas when and why my value for DEBUG is changing between settings and urls ? 任何想法何时以及为什么我的DEBUG价值在设置和网址之间发生变化?

From the docs 来自文档

Regardless of the value of the DEBUG setting in your configuration file, all Django tests run with DEBUG=False. 无论配置文件中DEBUG设置的值如何,所有Django测试都以DEBUG = False运行。 This is to ensure that the observed output of your code matches what will be seen in a production setting. 这是为了确保您观察到的代码输出与生产设置中的内容相匹配。

The problem with your code is you are setting DEBUG = True after this line 您的代码的问题是您在此行之后设置DEBUG = True

   urlpatterns += [url(r'^dnfp/$', dnfp, name="debug_not_found_page"

The reason is that all the URLs are already appended to urlpatterns[] and you are setting it after the appending of URLs and while appending the URL Django actually transfer control to urls.py for syntax validation purpose. 原因是所有URL都已附加到urlpatterns [],并且您在添加URL之后设置它,并且在附加URL时Django实际上将控制转移到urls.py以进行语法验证。 That's why you are getting different value in urls.py. 这就是为什么你在urls.py中得到不同的价值。

Set value of DEBUG before this line 在此行之前设置DEBUG的值

Try this, hope it will work. 试试这个,希望它能奏效。

You can use another approach for doing this, create a separate app for all these type of URLs and don't add the app to INSTALLED_APPS on the basis of debug variable. 您可以使用其他方法执行此操作,为所有这些类型的URL创建单独的应用程序,并且不要基于调试变量将应用程序添加到INSTALLED_APPS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM