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

Created on 21 Nov 2020  Â·  12Comments  Â·  Source: jazzband/django-debug-toolbar

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:

```#settings.py
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**:
```# urls.py
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
Support request

Most helpful comment

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.

All 12 comments

How does DEBUG get set in your settings file? It sounds like it's being set to True when you're running your tests. You can create a new settings file for your tests that specifically sets it to False. Or you can make sure that whatever mechanism is setting the value for DEBUG will set it to False when running the tests.

How does DEBUG get set in your settings file? It sounds like it's being set to True when you're running your tests. You can create a new settings file for your tests that specifically sets it to False. Or you can make sure that whatever mechanism is setting the value for DEBUG will set it to False when running the tests.

Hi Tim,
Thank you for your help. For the DEBUG setting, I just leave it as DEBUG=True in my settings.py file. I will make another setting file for testing. Just want to clear that, if I am wrong please let me know, DEBUG will not be set to False when we run a test?

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.

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.

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.

Awesome! Thanks @matthiask

@matthiask Do you think it's possible and worth us implementing a check that would prevent the toolbar from being instrumented if it's for a test run?

@tim-schilling I doubt there is an easy way to achieve this. We'd have to find solutions for pytest, unittest etc. The sys.argv hack definitely would not work for all cases.

Same error. Cannot get it to run.

Same error. Cannot get it to run.

Solved - Accidentally uploaded the urls.py of the project to the app directory of the server

Is this resolved?
I am getting this error as well. I followed installation steps mention here: https://django-debug-toolbar.readthedocs.io/en/latest/installation.html

Hi,

Can you please include your urls.py file?

On Thu, Jul 8, 2021, 12:21 AM Dip Dhanesha @.*> wrote:

Is this resolved?
I am getting this error as well. I followed installation steps mention
here:
https://django-debug-toolbar.readthedocs.io/en/latest/installation.html

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/jazzband/django-debug-toolbar/issues/1405#issuecomment-876136122,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAJYZP7FZI6I3VUTUPUIAM3TWUYVLANCNFSM4T55GDLA
.

@ddhanesha Looking at your code base, you need to inject the toolbar into your pragmatically add it to your installed apps and middleware on the same condition that you add it to your url patterns.

Was this page helpful?
0 / 5 - 0 ratings