Value of settings.DEBUG changing between settings and url in Django Test
<p>I'm trying to set up test for some URLS that are set only in debug. They are not set because apparently the value of DEBUG change to False between my setting file and urls.py. I've never encountered this problem before, and I don't remember doing anything particularly fancy involving DEBUG value.</p>

<p>Here's my urls.py :</p>

<pre><code>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"...
</code></pre>

<p>Here's my setting file :</p>

<pre><code>DEBUG=True
print "DEBUG at the end of the settings: {}".format(DEBUG)
</code></pre>

<p>The content that fail in my test :</p>

<pre><code> reverse("debug_not_found_page"), 
</code></pre>

<p>Here's the output of the test :</p>

<pre><code>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: []
</code></pre>

<p>If I change the value myself in urls.py the url is set again and the test works with this urls.py :</p>

<pre><code>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"...
</code></pre>

<p>Any ideas when and why my value for DEBUG is changing between settings and urls ?</p>

<p>The problem with your code is you are setting DEBUG = True after this line</p>

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

<p>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. That's why you are getting different value in urls.py.</p>

<p>Set value of DEBUG before this line</p>

<p>Try this, hope it will work.</p>

<p>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.</p>

python django testing