Django-debug-toolbar: DjDT reverses url before models are loaded in urls.py

Created on 14 Jan 2014  路  8Comments  路  Source: jazzband/django-debug-toolbar

Initially, I got this error when trying to set up a new project with Django Debug Toolbar and uwsgi:

NoReverseMatch at /

u'djdt' is not a registered namespace

This occurred in uwsgi first, which does not load the models beforehand. Later, was able to reproduce it by patching django runserver with the following:

diff --git a/django/core/management/commands/runserver.py b/django/core/management/commands/runserver.py
index 391e0b4..02dadc7 100644
--- a/django/core/management/commands/runserver.py
+++ b/django/core/management/commands/runserver.py
@@ -89,7 +89,7 @@ class Command(BaseCommand):
         quit_command = (sys.platform == 'win32') and 'CTRL-BREAK' or 'CONTROL-C'

         self.stdout.write("Validating models...\n\n")
-        self.validate(display_num_errors=True)
+        #self.validate(display_num_errors=True)
         self.stdout.write((
             "%(started_at)s\n"
             "Django version %(version)s, using settings %(settings)r\n"

I put a pdb before the url patching in models.py, and got this:

/usr/lib64/python2.7/threading.py(784)__bootstrap()
-> self.__bootstrap_inner()
  /usr/lib64/python2.7/threading.py(811)__bootstrap_inner()
-> self.run()
  /usr/lib64/python2.7/threading.py(764)run()
-> self.__target(*self.__args, *self.__kwargs)
  /usr/lib64/python2.7/SocketServer.py(593)process_request_thread()
-> self.finish_request(request, client_address)
  /usr/lib64/python2.7/SocketServer.py(334)finish_request()
-> self.RequestHandlerClass(request, client_address, self)
  django/core/servers/basehttp.py(150)__init__()
-> super(WSGIRequestHandler, self).__init__(*args, **kwargs)
  /usr/lib64/python2.7/SocketServer.py(649)__init__()
-> self.handle()
  /usr/lib64/python2.7/wsgiref/simple_server.py(124)handle()
-> handler.run(self.server.get_app())
  /usr/lib64/python2.7/wsgiref/handlers.py(85)run()
-> self.result = application(self.environ, self.start_response)
  django/contrib/staticfiles/handlers.py(72)__call__()
-> return self.application(environ, start_response)
  django/core/handlers/wsgi.py(255)__call__()
-> response = self.get_response(request)
  django/core/handlers/base.py(103)get_response()
-> resolver_match = resolver.resolve(request.path_info)
  django/core/urlresolvers.py(319)resolve()
-> for pattern in self.url_patterns:
  django/core/urlresolvers.py(347)url_patterns()
-> patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  django/core/urlresolvers.py(342)urlconf_module()
-> self._urlconf_module = import_module(self.urlconf_name)
  django/utils/importlib.py(35)import_module()
-> __import__(name)
  project/urls.py(5)<module>()
-> admin.autodiscover()
  mezzanine/boot/__init__.py(76)autodiscover()
-> django_autodiscover(*args, **kwargs)
  django/contrib/admin/__init__.py(29)autodiscover()
-> import_module('%s.admin' % app)
  django/utils/importlib.py(35)import_module()
-> __import__(name)
  django/contrib/auth/admin.py(179)<module>()
-> admin.site.register(Group, GroupAdmin)
  mezzanine/boot/lazy_admin.py(22)register()
-> super(LazyAdminSite, self).register(*args, **kwargs)
  django/contrib/admin/sites.py(98)register()
-> validate(admin_class, model)
  django/contrib/admin/validation.py(22)validate()
-> models.get_apps()
  django/db/models/loading.py(134)get_apps()
-> self._populate()
  django/db/models/loading.py(72)_populate()
-> self.load_app(app_name, True)
  django/db/models/loading.py(96)load_app()
-> models = import_module('.models', app_name)
  django/utils/importlib.py(35)import_module()
-> __import__(name)
  debug_toolbar/models.py(63)<module>()
-> patch_root_urlconf()
> debug_toolbar/models.py(51)patch_root_urlconf()
-> reverse('djdt:render_panel')
(Pdb) reverse('djdt:render_panel')
** ImproperlyConfigured: The included urlconf project.urls doesn't have any patterns in it

It seems like admin.autodiscover() loads the models in the urls.py, which subsequently forces debug_toolbar.models to reverse a url despite urls.py not having any urls in it yet.

Most helpful comment

@jonykwa Did you try adding this to your project's urls.py?

if settings.DEBUG:
    import debug_toolbar
    urlpatterns += patterns('',
        url(r'^__debug__/', include(debug_toolbar.urls)),
    )

All 8 comments

Please use the explicit setup described in the documentation. Unfortunately this won't work satisfactorily until Django 1.7 is released.

For others who land on this issue: Here is a link to the Explicit Setup : http://django-debug-toolbar.readthedocs.org/en/1.0/installation.html#explicit-setup

I still have this issue even after i add DEBUG_TOOLBAR_PATCH_SETTINGS = False to my settings.py, any more ideas?

+1 @jonykwa

@jonykwa Did you try adding this to your project's urls.py?

if settings.DEBUG:
    import debug_toolbar
    urlpatterns += patterns('',
        url(r'^__debug__/', include(debug_toolbar.urls)),
    )

I had to do both suggestions from @clarkbarz and @avelis as part of our Django 1.5->1.6 upgrade. Thanks!

Just to add some simple thing to check when you get this error. If you add the urlpattern before your own url patterns, don't forget to have your own urlpattern appended:

if settings.DEBUG:
    import debug_toolbar
    urlpatterns += [
        url(r'^__debug__/', include(debug_toolbar.urls)),
    ]
urlpatterns += [  # DON'T FORGET THE '+=' HERE!
.....  
]

Here settings.DEBUG is declared after my site url patterns, but making sure that the debug url pattern is before the site url patterns.

As seen in the docs for URLConf http://django-debug-toolbar.readthedocs.io/en/stable/installation.html#urlconf

from django.conf.urls import url, include
from django.contrib import admin
from django.conf import settings


urlpatterns = [
    url(r'^tinymce/', include('tinymce.urls')),
    url(r'^admin/', admin.site.urls),
]

if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
        url(r'^__debug__/', include(debug_toolbar.urls)),
    ] + urlpatterns
Was this page helpful?
0 / 5 - 0 ratings

Related issues

bengolder picture bengolder  路  6Comments

pawamoy picture pawamoy  路  5Comments

FlipperPA picture FlipperPA  路  6Comments

stfenjobs picture stfenjobs  路  6Comments

tim-schilling picture tim-schilling  路  7Comments