简体   繁体   中英

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

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', )

If you want the django debug toolbar to be seen. If it is django 1.11 , then please try the below code. Replace the "patterns" with [] (list). I tested in django-debug-toolbar 1.8

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

Please check on other things like

  1. DEBUG = True

  2. MIDDLEWARE_CLASSES = ['debug_toolbar.middleware.DebugToolbarMiddleware',]

  3. INTERNAL_IPS = ['127.0.0.1',] (add the ip's accordingly)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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