Denise Mauldin Denise Mauldin - 1 year ago 128
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 manage.py 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/base.py", 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/exception.py", line 41, in inner
response = get_response(request)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/utils/deprecation.py", line 142, in __call__
response = self.process_response(request, response)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/debug_toolbar/middleware.py", line 134, in process_response
bits[-2] += toolbar.render_toolbar()
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/debug_toolbar/toolbar.py", 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/loader.py", line 68, in render_to_string
return template.render(context, request)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/test/utils.py", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/vagrant/.virtualenvs/ann/lib/python3.5/site-packages/django/template/defaulttags.py", 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/base.py", 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