37

My question is about setting up to use django-debug. I'm getting the above error after installing the toolbar and panel, and enabling these in my app. I've seen many suggestions for this or a closely related issue, and nothing I've tried has helped.

The specific error, during template rendering of /usr/lib/python3.6/site-packages/debug_toolbar/templates/debug_toolbar/base.html, is from:

16       data-render-panel-url="{% url 'djdt:render_panel' %}"

My relevant settings.py entries:

DEBUG = True
INSTALLED_APPS = [
    'debug_toolbar',
    'debug_panel',
    ...
]
MIDDLEWARE = [
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'debug_panel.middleware.DebugPanelMiddleware',
    ...
]
INTERNAL_IPS = ['127.0.0.1',]

Appended to my urls.py:

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

What I've tried:

  • changing the order of these Middleware entries in settings.py (first, middle and last)
  • adding a namespace attribute to my urlpatterns entry

Thanks for any further suggestions.

CodeMantle
  • 1,249
  • 2
  • 16
  • 25

5 Answers5

44

You need to manually add 'djdt' routes to the end of urls.py (if you use 'namespace' in your apps, add below codes to 'urls.py' in your project):

if settings.DEBUG:
    import debug_toolbar

    urlpatterns += [
        url(r'^__debug__/', include(debug_toolbar.urls)),
    ]
Richard de Wit
  • 7,102
  • 7
  • 44
  • 54
Milad Hatami
  • 1,494
  • 13
  • 18
10

if this problem occurs when we set DEBUG to False then simply removing debugtoolbar middleware from the list fixed the problem.

  • settings.py
MIDDLEWARE = [
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

if DEBUG is False:
    del MIDDLEWARE[0]
  • urls.py
from django.conf import settings
from django.conf.urls import include

...

if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
        path('__debug__/', include(debug_toolbar.urls)),
    ] + urlpatterns
Tiago Martins Peres
  • 14,289
  • 18
  • 86
  • 145
Arbazz Hussain
  • 1,622
  • 2
  • 15
  • 41
  • 2
    This would be okay but it will be error if the first item of the MIDDLEWARE is not of 'debug_toolbar'. – Dat TT Feb 25 '20 at 04:48
1

The error is due to we declare its middle but the module is not import in case DEBUG = FALSE. So, just check to add its middle in case DEDEUG is set TRUE

settings.py

MIDDLEWARE = [
    ...
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
....
]

if DEBUG:
    
    MIDDLEWARE = ['debug_toolbar.middleware.DebugToolbarMiddleware'] + MIDDLEWARE

urls.py

if settings.DEBUG:
    import debug_toolbar
    urlpatterns = [
        path('__debug__/', include(debug_toolbar.urls)),
    ] + urlpatterns
thiras
  • 521
  • 1
  • 6
  • 22
Dat TT
  • 2,850
  • 2
  • 16
  • 18
1

Please make sure that you put the following at the end of the project urls.py not the app urls.py

if settings.DEBUG:
    import debug_toolbar

    urlpatterns += [
        path('__debug__/', include(debug_toolbar.urls)),
    ]

also make sure to add the following in settings.py

if DEBUG:

    MIDDLEWARE += (
        'debug_toolbar.middleware.DebugToolbarMiddleware',
    )
    INSTALLED_APPS += (
        'debug_toolbar',
    )
    INTERNAL_IPS = ('127.0.0.1',)
    DEBUG_TOOLBAR_CONFIG = {
        'INTERCEPT_REDIRECTS': False,
    }
-1

Seems like you are using both debug_toolbar and debug_panel And in the documentation for debug panel it is mentioned you need to remove debug_toolbar.

Replace the Django Debug Toolbar middleware with the Django Debug Panel one.

Replace:

MIDDLEWARE_CLASSES = (
    ...
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    ...
)

with:

MIDDLEWARE_CLASSES = (
    ...
    'debug_panel.middleware.DebugPanelMiddleware',
    ...
)

I would recommend not using django-debug-panel since it is not actively maintained. It was last modified 3 years back.

Arghya Saha
  • 5,599
  • 4
  • 26
  • 48
  • Thanks for response, but your recommended change did not work, I've been shuffling debug-toolbar/panel alternatives in/out for some time. If panel is not recommended (and presumably also toolbar), what is the alternative to debug models/views/etc in Django? – CodeMantle Aug 23 '18 at 12:48
  • why don't you use pdb? – Arghya Saha Aug 23 '18 at 13:42
  • Yes, that seems like the best way to debug problems, per [this](https://stackoverflow.com/questions/1118183/how-to-debug-in-django-the-good-way) stackoverflow question/answers. I'm particularly interested in the Visual Studio recommendation. – CodeMantle Aug 23 '18 at 14:27
  • Unfortunately I have only worked with pycharm, so I'm unaware of visual studio – Arghya Saha Aug 23 '18 at 14:30
  • rather than see unwarranted down-votes on your answer, I'll uncheck it so that I can give correct answer to Milad (higher votes). I've moved on to Flask, so not in a position to check. – CodeMantle Jan 04 '20 at 08:22