django-debug-toolbar not showing up

前端 未结 27 1652
谎友^
谎友^ 2020-11-30 20:12

I looked at other questions and can\'t figure it out...

I did the following to install django-debug-toolbar:

  1. pip install django-debug-toolbar
27条回答
  •  天命终不由人
    2020-11-30 21:11

    Docker

    If you're developing with a Django server in a Docker container with docker, the instructions for enabling the toolbar don't work. The reason is related to the fact that the actual address that you would need to add to INTERNAL_IPS is going to be something dynamic, like 172.24.0.1. Rather than trying to dynamically set the value of INTERNAL_IPS, the straightforward solution is to replace the function that enables the toolbar, in your settings.py, for example:

    DEBUG_TOOLBAR_CONFIG = {
        'SHOW_TOOLBAR_CALLBACK': lambda _request: DEBUG
    }
    


    This should also work for other dynamic routing situations, like vagrant.


    Here are some more details for the curious. The code in django_debug_tool that determines whether to show the toolbar examines the value of REMOTE_ADDR like this:

    if request.META.get('REMOTE_ADDR', None) not in INTERNAL_IPS:
           return False
    

    so if you don't actually know the value of REMOTE_ADDR due to your dynamic docker routing, the toolbar will not work. You can use the docker network command to see the dynamic IP values, for example docker network inspect my_docker_network_name

提交回复
热议问题