Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@elgartoinf elgartoinf commented Aug 18, 2017

thank you very much

descarga

@carrypandin

This comment has been minimized.

Copy link

@carrypandin carrypandin commented Nov 10, 2017

thank you.. this fix my problem..

@lily524

This comment has been minimized.

Copy link

@lily524 lily524 commented Dec 1, 2017

save me a lot of time. thanks!

@scandiumby

This comment has been minimized.

Copy link

@scandiumby scandiumby commented Mar 15, 2018

Thanks! It is fix my problem!

@NorakGithub

This comment has been minimized.

Copy link

@NorakGithub NorakGithub commented Jun 30, 2018

Great!!

@gyermolenko

This comment has been minimized.

Copy link

@gyermolenko gyermolenko commented Jul 20, 2018

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

This comment has been minimized.

Copy link

@diek diek commented Mar 29, 2019

Awesome thanks!

@martinsvoboda

This comment has been minimized.

Copy link

@martinsvoboda 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

This comment has been minimized.

Copy link

@rhonaldmoses rhonaldmoses commented Sep 19, 2019

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

This comment has been minimized.

Copy link
Owner Author

@douglasmiranda douglasmiranda commented Sep 19, 2019

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

This comment has been minimized.

Copy link

@rhonaldmoses rhonaldmoses commented Sep 20, 2019

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

This comment has been minimized.

Copy link

@mattpavelle mattpavelle commented Dec 3, 2019

worked perfectly, thanks.

@nffdiogosilva

This comment has been minimized.

Copy link

@nffdiogosilva nffdiogosilva commented Mar 31, 2020

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

@adamb70

This comment has been minimized.

Copy link

@adamb70 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

This comment has been minimized.

Copy link

@cgillions 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()
    }
@mcllvz

This comment has been minimized.

Copy link

@mcllvz mcllvz commented May 4, 2020

WHoop, gracias!

@daniel-butler

This comment has been minimized.

Copy link

@daniel-butler daniel-butler commented May 22, 2020

yay!!

@spyker77

This comment has been minimized.

Copy link

@spyker77 spyker77 commented Jun 16, 2020

@cgillions many thanks!

@uliSchuster

This comment has been minimized.

Copy link

@uliSchuster uliSchuster commented Oct 21, 2020

Nice!

@YorbenVerhoest

This comment has been minimized.

Copy link

@YorbenVerhoest YorbenVerhoest commented May 7, 2021

Thanks!

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