Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Last active April 25, 2024 09:10
Show Gist options
  • Star 40 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save douglasmiranda/9de51aaba14543851ca3 to your computer and use it in GitHub Desktop.
Save douglasmiranda/9de51aaba14543851ca3 to your computer and use it in GitHub Desktop.
Fix: Django Debug Toolbar not showing when using with Docker.
# YOU MAY WANT TO CHECK THIS OUT: https://github.com/douglasmiranda/ddpt/blob/master/{{cookiecutter.django_project_name}}/{{cookiecutter.django_project_name}}/config/local.py
# If you don't do this you will have to add the host IP in INTERNAL_IPS = ('127.0.0.1',)
# And it will change, then you will have to change INTERNAL_IPS again.
def show_toolbar(request):
if request.is_ajax():
return False
return True
DEBUG_TOOLBAR_CONFIG = {
'SHOW_TOOLBAR_CALLBACK': 'config.local.show_toolbar',
}
# Depending on your requirements, you can do:
import socket
import os
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[:-1] + '1' for ip in ips] + ['127.0.0.1', '10.0.2.2']
@elgartoinf
Copy link

thank you very much

descarga

@carrypandin
Copy link

thank you.. this fix my problem..

@lily524
Copy link

lily524 commented Dec 1, 2017

save me a lot of time. thanks!

@scandiumby
Copy link

Thanks! It is fix my problem!

@NorakGithub
Copy link

Great!!

@gyermolenko
Copy link

ip[:-1] + '1' replaces not the last octet but the last digit of the last octet. Is it intended? If so, could you please explain why it should fix an issue?

@diek
Copy link

diek commented Mar 29, 2019

Awesome thanks!

@martinsvoboda
Copy link

martinsvoboda commented May 14, 2019

Thanks! Just watch out if gethostbyname_ex returns IP address with the last octet longer than 1 character. Probably is more safe use something like this:

import socket
INTERNAL_IPS = ['127.0.0.1', '::1', 'localhost']

# get ip address for docker host
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
for ip in ips:
    # replace last octet in IP with .1
    ip = '{}.1'.format(ip.rsplit('.', 1)[0])
    INTERNAL_IPS.append(ip)

@rhonaldmoses
Copy link

Still not showing;

my settings.py

def show_toolbar(request):
    if DEBUG:
        return True
    return False

DEBUG_TOOLBAR_CONFIG = {
    "SHOW_TOOLBAR_CALLBACK" : show_toolbar,
}

hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[:-1] + '1' for ip in ips] + ['127.0.0.1', 'x.x.x.x','x.x.x.x']

The toolbar wouldn't show when I run on docker.

@douglasmiranda
Copy link
Author

Hey @rhonaldmoses take a look in a more up to date code:

INSTALLED_APPS += ['debug_toolbar', ]
MIDDLEWARE = ['debug_toolbar.middleware.DebugToolbarMiddleware'] + MIDDLEWARE
hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[:-1] + '1' for ip in ips] + ['127.0.0.1', '10.0.2.2']

Not using SHOW_TOOLBAR_CALLBACK anymore.

From douglasmiranda/ddpt.

@rhonaldmoses
Copy link

Unfortunately it still wouldn't show up. This is what my settings.py looks like

INSTALLED_APPS = [
...
'debug_toolbar',
]

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

hostname, _, ips = socket.gethostbyname_ex(socket.gethostname())
INTERNAL_IPS = [ip[:-1] + '1' for ip in ips] + ['127.0.0.1', '10.0.2.2']

@mattpavelle
Copy link

worked perfectly, thanks.

@nffdiogosilva
Copy link

@douglasmiranda, boss! Thank you so much, It helped me a lot ;)

@adamb70
Copy link

adamb70 commented Apr 23, 2020

Smaller one line version:

DEBUG_TOOLBAR_CONFIG = {
    'SHOW_TOOLBAR_CALLBACK': lambda request: False if request.is_ajax() else True,
}

@cgillions
Copy link

cgillions commented May 1, 2020

Thank you for making us aware that the is_ajax function does what we're looking for @adamb70!

Using this lambda, we can avoid specifying INTERNAL_IPS.

Here's my full solution:

# Determine debug state.
DEBUG = os.getenv("DEBUG", "True") == "True"

# Add the debug toolbar.
if DEBUG:
    INSTALLED_APPS = INSTALLED_APPS + ['debug_toolbar']
    MIDDLEWARE = ['debug_toolbar.middleware.DebugToolbarMiddleware'] + MIDDLEWARE

    DEBUG_TOOLBAR_CONFIG = {
        'SHOW_TOOLBAR_CALLBACK': lambda request: not request.is_ajax()
    }

@mllavez
Copy link

mllavez commented May 4, 2020

WHoop, gracias!

@daniel-butler
Copy link

yay!!

@spyker77
Copy link

@cgillions many thanks!

@uliSchuster
Copy link

Nice!

@YorbenVerhoest
Copy link

Thanks!

@matacoder
Copy link

Smaller one line version:

DEBUG_TOOLBAR_CONFIG = {
    'SHOW_TOOLBAR_CALLBACK': lambda request: False if request.is_ajax() else True,
}

This is everything you need to add to standard config. They should add it in Installation guide

@Leonmcnamara
Copy link

Thank you matacoder. Your solution did the trick for me.

@kbehlers
Copy link

kbehlers commented Dec 2, 2021

is_ajax appears to be deprecated in Django 3.1

@imawmir
Copy link

imawmir commented Dec 4, 2021

Great thank u so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment