From 08f858bec1903cf56dd809bc2edcbc32f32962b0 Mon Sep 17 00:00:00 2001 From: Friedel Wolff Date: Thu, 1 Aug 2024 11:49:36 +0200 Subject: [PATCH] Rework DEBUG_TOOLBAR_CONFIG MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ... by removing it. The setting for SHOW_TOOLBAR_CALLBACK seems to have been an attempt to work around issues when running inside docker. Debug toolbar tries to handle this case, but it doesn't work sufficiently (at least in my case). The documentation has an opinion on this matter: Do not use DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": lambda request: DEBUG} in your project’s settings.py file. This commit is longer than I'd like, but the setting of INTERNAL_IPS seems to really fix it, and solves the fact that the debug context processor wasn't working either. With this fixed, {% debug %} works in templates. INTERNAL_IPS should best contain IP addresses rather than hostnames. IS_RUNNING_TESTS... no idea why we would want the toolbar while running tests. --- app/app/settings.py | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/app/app/settings.py b/app/app/settings.py index dd70f369..1f767c69 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -118,16 +118,41 @@ } } -# toolbar settings if DEBUG: - DEBUG_TOOLBAR_CONFIG = { - "IS_RUNNING_TESTS": False, - "SHOW_TOOLBAR_CALLBACK": lambda request: DEBUG, - } + # Some things rely on the setting INTERNAL_IPS: + # - debug_toolbar.middleware.show_toolbar + # - django.template.context_processors.debug + # See https://docs.djangoproject.com/en/stable/ref/settings/#internal-ips + # Inside a docker container, it isn't trivial to get the IP address of the + # Docker host that will appear in REMOTE_ADDR. The following seems to work + # for now to add support for a range of IP addresses without having to put + # a huge list in INTERNAL_IPS, e.g. with + # map(str, ipaddress.ip_network('172.0.0.0/24')) + # If this can't resolve the name "host.docker.internal", nothing is added + # to INTERNAL_IPS. + import socket + + try: + ip = socket.gethostbyname("host.docker.internal") + except socket.gaierror: + # presumably not in docker + ip = None + + import ipaddress + + # Based on https://code.djangoproject.com/ticket/3237#comment:12 + class CIDRList(list): + def __init__(self, addresses): + """Create a new ip_network object for each address range provided.""" + self.networks = [ipaddress.ip_network(address, strict=False) for address in addresses] + + def __contains__(self, address): + """Check if the given address is contained in any of the networks.""" + return any([ipaddress.ip_address(address) in network for network in self.networks]) + + if ip: + INTERNAL_IPS = CIDRList([f"{ip}/8"]) - INTERNAL_IPS = [ - "host.docker.internal", - ] # Email settings EMAIL_HOST = os.environ.get("EMAIL_HOST")