Browning M.
Browning M.

Reputation: 451

Django can' t load Module 'debug_toolbar': No module named 'debug_toolbar'

When I try running the project, Django can not load the django-debug-toolbar plugin for some reason. Error message says:

web_1  | ModuleNotFoundError: No module named 'debug_toolbar'

Here is my settings.py

INSTALLED_APPS = [
    # ...
    'django.contrib.staticfiles',
    # ...
    'debug_toolbar',
]

MIDDLEWARE = [
    # ...
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    # ...
]

INTERNAL_IPS = ('127.0.0.1', '192.168.0.1',)

Upvotes: 23

Views: 46521

Answers (7)

Likid
Likid

Reputation: 34

If you installed django-debug-toolbar in a virtual environment, it will work only if that virtual environment is activated ;)

Upvotes: 0

spiffytech
spiffytech

Reputation: 6602

I resolved the import error by moving the debug toolbar middleware to after all of the default Django middlewares:

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',
    'debug_toolbar.middleware.DebugToolbarMiddleware' # <-- HERE
]

With that change, the toolbar didn't display. That's because:

It will also only display if the MIME type of the response is either text/html or application/xhtml+xml and contains a closing tag.

My page didn't have a </body> closing tag, since the Django tutorial introduced the debug toolbar before introducing page layouts.

I added <body> / </body> to my page, and the toolbar then displayed.

Upvotes: 0

Harlin
Harlin

Reputation: 1149

I ran across this issue because we're using docker containers for the development environment and we're also using Pipenv to manage deps. Silly me, I did:

pipenv install django-debug-toolbar --dev

When in my environment this does no good because it will never be installed on my docker container environment. I had to reinstall without the --dev part and it worked fine afterwards.

Hope this helps someone out in the same situation.

Upvotes: 0

vinodsesetti
vinodsesetti

Reputation: 418

If you have not installed the django-debug-toolbar package you can install it with help of the below command:

pip install django-debug-toolbar

Upvotes: 31

Browning M.
Browning M.

Reputation: 451

I had to re-install django-debug-toolbar by adding it to requirements.txt and then running:

docker-compose build web

After doing that, the toolbar wasn't still showing. I had to add this code to the settings.py file

def show_toolbar(request):
    return True

DEBUG_TOOLBAR_CONFIG = {
  "SHOW_TOOLBAR_CALLBACK" : show_toolbar,
}

Answered here: https://stackoverflow.com/a/10518040/11011598

Upvotes: 6

jbcloud29
jbcloud29

Reputation: 31

You can use the below command which worked perfectly for me:

$ python -m pip install -e git+https://github.com/jazzband/django-debug-toolbar.git#egg=django-debug-toolbar

Upvotes: 2

Filip Skiba
Filip Skiba

Reputation: 11

I did run across the same problem when trying to use debug toolbar with Django & docker.

The solution posted above did not solve the problem entirely. The toolbar was sometimes not visible on the rest_framework browsable API.

I solved the problem by adding the below into my settings.py

INTERNAL_IPS = ['127.0.0.1',"0.0.0.0:8000"] 

import socket
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS += [".".join(ip.split(".")[:-1] + ["1"]) for ip in ips]

ref: https://knasmueller.net/fix-djangos-debug-toolbar-not-showing-inside-docker

Upvotes: 1

Related Questions