django-debug-toolbar: 'djdt' is not a registered namespace

I got this error when I run django unittest, I have tried all the answers I could find, seems not working. Here are my settings:

DEBUG_TOOLBAR_PATCH_SETTINGS = False
if DEBUG:
    INSTALLED_APPS += ['debug_toolbar']
    MIDDLEWARE += ['debug_toolbar.middleware.DebugToolbarMiddleware', ]
    DEBUG_TOOLBAR_CONFIG = {
        'SHOW_TOOLBAR_CALLBACK': lambda _request: DEBUG
    }

Project url file:

if settings.DEBUG:
    import debug_toolbar
    urlpatterns += [path('__debug__/', include(debug_toolbar.urls)), ]\
        + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

If I turn off the debug to False manually, then the error is gone, if I just run manage.py test with all those settings above or either same as the official doc , then I get the this error. Is there any method to avoid this error? Doesn’t the debug suppose to be False when running a test?

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 829, in _resolve_lookup
    current = current[bit]
TypeError: 'HistoryPanel' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/django/urls/base.py", line 72, 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 "/usr/src/app/apps/shop/tests/test_views.py", line 63, in test_shop_list_view_sort_GET
    res = self.client.get(reverse('shop:product-list')+'?sorting=sales')
  File "/usr/local/lib/python3.8/site-packages/django/test/client.py", line 733, in get
    response = super().get(path, data=data, secure=secure, **extra)
  File "/usr/local/lib/python3.8/site-packages/django/test/client.py", line 393, in get
    return self.generic('GET', path, secure=secure, **{
  File "/usr/local/lib/python3.8/site-packages/django/test/client.py", line 470, in generic
    return self.request(**r)
  File "/usr/local/lib/python3.8/site-packages/django/test/client.py", line 710, in request
    self.check_exception(response)
  File "/usr/local/lib/python3.8/site-packages/django/test/client.py", line 571, in check_exception
    raise exc_value
  File "/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.8/site-packages/debug_toolbar/middleware.py", line 94, in __call__
    bits[-2] += toolbar.render_toolbar()
  File "/usr/local/lib/python3.8/site-packages/debug_toolbar/toolbar.py", line 69, in render_toolbar
    return render_to_string("debug_toolbar/base.html", context)
  File "/usr/local/lib/python3.8/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/usr/local/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 170, in render
    return self._render(context)
  File "/usr/local/lib/python3.8/site-packages/django/test/utils.py", line 96, in instrumented_test_render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/defaulttags.py", line 211, in render
    nodelist.append(node.render_annotated(context))
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/loader_tags.py", line 192, in render
    return template.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 172, in render
    return self._render(context)
  File "/usr/local/lib/python3.8/site-packages/django/test/utils.py", line 96, in instrumented_test_render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/defaulttags.py", line 312, in render
    return nodelist.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/defaulttags.py", line 312, in render
    return nodelist.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 988, in render
    output = self.filter_expression.resolve(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 671, in resolve
    obj = self.var.resolve(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 796, in resolve
    value = self._resolve_lookup(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 837, in _resolve_lookup
    current = getattr(current, bit)
  File "/usr/local/lib/python3.8/site-packages/debug_toolbar/panels/history/panel.py", line 90, in content
    return render_to_string(
  File "/usr/local/lib/python3.8/site-packages/django/template/loader.py", line 62, in render_to_string
    return template.render(context, request)
  File "/usr/local/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 170, in render
    return self._render(context)
  File "/usr/local/lib/python3.8/site-packages/django/test/utils.py", line 96, in instrumented_test_render
    return self.nodelist.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 938, in render
    bit = node.render_annotated(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/base.py", line 905, in render_annotated
    return self.render(context)
  File "/usr/local/lib/python3.8/site-packages/django/template/defaulttags.py", line 446, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
  File "/usr/local/lib/python3.8/site-packages/django/urls/base.py", line 83, in reverse
    raise NoReverseMatch("%s is not a registered namespace" % key)
django.urls.exceptions.NoReverseMatch: 'djdt' is not a registered namespace

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 25 (15 by maintainers)

Commits related to this issue

Most upvoted comments

Here’s the way I do it these days. I have a DEBUG and a TESTING setting: https://github.com/matthiask/workbench/blob/5e3663cb7c28cec707cfe746e9072e4fa6778f30/workbench/settings.py#L16-L17

Then, I initialize DEBUG_TOOLBAR = DEBUG and not TESTING

Now Django (and other packages) can change DEBUG as much as they want, debug toolbar is either activated completely or not at all.

Hi there, I have recently started working on a large project and have tried adding django-debug-toolbar. The setup works perfectly, however when any unit tests that make use of the django test client to make requests to views run, I receive the 'djdt' is not a registered namespace error.

I read that when running unit tests django explicitly sets DEBUG to false (See here).

I have begun debugging the issue and it seems the following happens when the unit tests are run:

  1. The settings module in our project directory is executed. At this point in time DEBUG is True as it is read from a env variable for local development
  2. The test runner calls setup_test_environment which then sets DEBUG to False and runs the tests
  3. At this point the debug toolbar has been setup however when the tests run the debug-toolbar urls are never added to the project url patterns since DEBUG is now false. This results in the namespace error

Is the use of the django test client a known issue with debug toolbar? As I mentioned above the project is quite large and there may be something else causing the issue.

In the meantime I am going to provide a environment variable specifically for the setup of the debug-toolbar.

@martinberoiz what’s going on is what is explained here https://github.com/jazzband/django-debug-toolbar/issues/1405#issuecomment-733689292

DEBUG is actually True when settings load, so MIDDLEWARE and INSTALLED_APPS are setup with the toolbar, then the test runner flips debug to false. However, MIDDLEWARE and INSTALLED_APPS are already populated with the debug settings. The solution is to use Matthias’ approach as defined here: https://github.com/jazzband/django-debug-toolbar/issues/1405#issuecomment-733769171

See https://github.com/matthiask/workbench/blob/5e3663cb7c28cec707cfe746e9072e4fa6778f30/workbench/settings.py#L16-L17 for an example of how he sets them.