Skip to content

Commit bdfb210

Browse files
Allow enabling Django Debug Toolbar via env variable
1 parent 2a252ca commit bdfb210

File tree

4 files changed

+53
-49
lines changed

4 files changed

+53
-49
lines changed

dojo/settings/settings.dist.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
# Set casting and default values
3030
DD_SITE_URL=(str, "http://localhost:8080"),
3131
DD_DEBUG=(bool, False),
32+
DD_DJANGO_DEBUG_TOOLBAR_ENABLED=(bool, False),
3233
DD_TEMPLATE_DEBUG=(bool, False),
3334
DD_LOG_LEVEL=(str, ""),
3435
DD_DJANGO_METRICS_ENABLED=(bool, False),
@@ -356,6 +357,7 @@ def generate_url(scheme, double_slashes, user, password, host, port, path, param
356357

357358
# False if not in os.environ
358359
DEBUG = env("DD_DEBUG")
360+
DJANGO_DEBUG_TOOLBAR_ENABLED = env("DD_DJANGO_DEBUG_TOOLBAR_ENABLED")
359361
TEMPLATE_DEBUG = env("DD_TEMPLATE_DEBUG")
360362

361363
# Hosts/domain names that are valid for this site; required if DEBUG is False
@@ -1940,3 +1942,45 @@ def saml2_attrib_map_format(din):
19401942
warnings.filterwarnings("ignore", "The FORMS_URLFIELD_ASSUME_HTTPS transitional setting is deprecated.")
19411943
FORMS_URLFIELD_ASSUME_HTTPS = True
19421944
# Inspired by https://adamj.eu/tech/2023/12/07/django-fix-urlfield-assume-scheme-warnings/
1945+
1946+
if DEBUG:
1947+
# adding DEBUG logging for all of Django.
1948+
LOGGING["loggers"]["root"] = {
1949+
"handlers": ["console"],
1950+
"level": "DEBUG",
1951+
}
1952+
1953+
if DJANGO_DEBUG_TOOLBAR_ENABLED:
1954+
1955+
INSTALLED_APPS += (
1956+
"debug_toolbar",
1957+
)
1958+
1959+
MIDDLEWARE = ["debug_toolbar.middleware.DebugToolbarMiddleware", *MIDDLEWARE]
1960+
1961+
def show_toolbar(request):
1962+
return True
1963+
1964+
DEBUG_TOOLBAR_CONFIG = {
1965+
"SHOW_TOOLBAR_CALLBACK": show_toolbar,
1966+
"INTERCEPT_REDIRECTS": False,
1967+
"SHOW_COLLAPSED": True,
1968+
}
1969+
1970+
DEBUG_TOOLBAR_PANELS = [
1971+
# 'ddt_request_history.panels.request_history.RequestHistoryPanel', # Here it is
1972+
"debug_toolbar.panels.versions.VersionsPanel",
1973+
"debug_toolbar.panels.timer.TimerPanel",
1974+
"debug_toolbar.panels.settings.SettingsPanel",
1975+
"debug_toolbar.panels.headers.HeadersPanel",
1976+
"debug_toolbar.panels.request.RequestPanel",
1977+
"debug_toolbar.panels.sql.SQLPanel",
1978+
"debug_toolbar.panels.templates.TemplatesPanel",
1979+
# 'debug_toolbar.panels.staticfiles.StaticFilesPanel',
1980+
"debug_toolbar.panels.cache.CachePanel",
1981+
"debug_toolbar.panels.signals.SignalsPanel",
1982+
# 'debug_toolbar.panels.logging.LoggingPanel',
1983+
"debug_toolbar.panels.redirects.RedirectsPanel",
1984+
"debug_toolbar.panels.profiling.ProfilingPanel",
1985+
# 'cachalot.panels.CachalotPanel',
1986+
]

dojo/settings/template-local_settings

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
# local_settings.py
22
# this file will be included by settings.py *after* loading settings.dist.py
33

4-
# this example configures the django debug toolbar and sets some loglevels to DEBUG
5-
6-
from django.urls import re_path
7-
from django.conf.urls import include
8-
9-
# UPDATE: Adding debug_toolbar to to INSTALLED_APPS here prevents the nginx container from generating the correct static files
10-
# So add debug_toolbar to INSTALLED_APPS in settings.dist.py and rebuild to get started with the debug_toolbar.
11-
# Thje middleware and other config can remain in this file (local_settings.py) to avoid chance of conflicts on upgrades.
12-
INSTALLED_APPS += (
13-
# 'debug_toolbar',
14-
)
15-
16-
MIDDLEWARE = [
17-
'debug_toolbar.middleware.DebugToolbarMiddleware',
18-
] + MIDDLEWARE
4+
# this example sets some loglevels to DEBUG
195

206
# adding DEBUG logging for all of Django.
217
LOGGING['loggers']['root'] = {
@@ -27,35 +13,3 @@ LOGGING['loggers']['root'] = {
2713

2814
# output DEBUG logging for deduplication
2915
# LOGGING['loggers']['dojo.specific-loggers.deduplication']['level'] = 'DEBUG'
30-
31-
32-
def show_toolbar(request):
33-
return True
34-
35-
36-
DEBUG_TOOLBAR_CONFIG = {
37-
"SHOW_TOOLBAR_CALLBACK": show_toolbar,
38-
"INTERCEPT_REDIRECTS": False,
39-
"SHOW_COLLAPSED": True,
40-
}
41-
42-
DEBUG_TOOLBAR_PANELS = [
43-
# 'ddt_request_history.panels.request_history.RequestHistoryPanel', # Here it is
44-
'debug_toolbar.panels.versions.VersionsPanel',
45-
'debug_toolbar.panels.timer.TimerPanel',
46-
'debug_toolbar.panels.settings.SettingsPanel',
47-
'debug_toolbar.panels.headers.HeadersPanel',
48-
'debug_toolbar.panels.request.RequestPanel',
49-
'debug_toolbar.panels.sql.SQLPanel',
50-
'debug_toolbar.panels.templates.TemplatesPanel',
51-
# 'debug_toolbar.panels.staticfiles.StaticFilesPanel',
52-
'debug_toolbar.panels.cache.CachePanel',
53-
'debug_toolbar.panels.signals.SignalsPanel',
54-
'debug_toolbar.panels.logging.LoggingPanel',
55-
'debug_toolbar.panels.redirects.RedirectsPanel',
56-
'debug_toolbar.panels.profiling.ProfilingPanel',
57-
# 'cachalot.panels.CachalotPanel',
58-
]
59-
60-
import debug_toolbar
61-
EXTRA_URL_PATTERNS = [re_path(r"^__debug__/", include(debug_toolbar.urls))]

dojo/urls.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,9 @@ def drf_spectacular_preprocessing_filter_spec(endpoints):
281281
if path.startswith("/api/v2/"):
282282
filtered.append((path, path_regex, method, callback))
283283
return filtered
284+
285+
286+
if hasattr(settings, "DJANGO_DEBUG_TOOLBAR_ENABLED"):
287+
if settings.DEBUG_TOOLBAR_ENABLED:
288+
from debug_toolbar.toolbar import debug_toolbar_urls
289+
urlpatterns += debug_toolbar_urls()

readme-docs/DOCKER.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ Or you can modify `settings.dist.py` directly, but this adds the risk of having
176176
```
177177

178178
## Debug Toolbar
179-
In the `dojo/settings/template-local_settings.py` you'll find instructions on how to enable the [Django Debug Toolbar](https://github.com/jazzband/django-debug-toolbar).
180-
This toolbar allows you to debug SQL queries, and shows some other interesting information.
179+
The [Django Debug Toolbar](https://github.com/jazzband/django-debug-toolbar) can be enabled via the `DD_DJANGO_DEBUG_TOOLBAR_ENABLED` environment variable.
180+
This toolbar allows you to debug SQL queries, and shows some other interesting information. Do NOT enable this in Production environments.
181181

182182

183183
# Explicit Versioning

0 commit comments

Comments
 (0)