Denise Mauldin Denise Mauldin - 2 years ago 178
Python Question

Selenium and django debug toolbar

My website with Django 1.11.1 and Django debug toolbar 1.8 runs fine on my development server (vagrant/gunicorn/nginx). My regular test suite runs fine. However, when I tell it to run the selenium tests, it dies with a

'djdt' is not a registered namespace error
. I have the appropriate entries in urls, middleware, and config. If I curl localhost, it returns the home page with the debug toolbar in it. If I set the toolbar not to show with the show_toolbar code, my selenium tests still crash, but the website still works fine (and doesn't show the debug toolbar).

Thoughts on how I can get my selenium tests to run?

> python test ann.selenium
Creating test database for alias 'default'...
Installed 8 object(s) from 1 fixture(s)
Installed 3 object(s) from 1 fixture(s)
Installed 2 object(s) from 1 fixture(s)
Installed 16 object(s) from 7 fixture(s)
Installed 6 object(s) from 1 fixture(s)
Installed 1 object(s) from 1 fixture(s)
Adding auth tokens for the API...
System check identified no issues (0 silenced).
Internal Server Error: /
Traceback (most recent call last):
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/urls/", line 77, in reverse
extra, resolver = resolver.namespace_dict[ns]
KeyError: 'djdt'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/core/handlers/", line 41, in inner
response = get_response(request)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/utils/", line 142, in __call__
response = self.process_response(request, response)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/debug_toolbar/", line 134, in process_response
bits[-2] += toolbar.render_toolbar()
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/debug_toolbar/", line 64, in render_toolbar
return render_to_string('debug_toolbar/base.html', context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/", line 68, in render_to_string
return template.render(context, request)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/backends/", line 66, in render
return self.template.render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/", line 207, in render
return self._render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/test/", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/", line 458, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/urls/", line 87, in reverse
raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'djdt' is not a registered namespace

Answer Source

Apparently the answer is to not load django_debug_toolbar if you're doing selenium testing.

import sys
TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'

if not TESTING:
    MIDDLEWARE += ('debug_toolbar.middleware.DebugToolbarMiddleware',)
    INSTALLED_APPS += ('debug_toolbar', )
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download