Skip to content

Commit

Permalink
Turn Django DebugToolbar off in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nickjj committed Jun 6, 2024
1 parent 3b3d732 commit 02db3ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import os
import socket
import sys
from distutils.util import strtobool
from pathlib import Path

Expand All @@ -22,14 +23,15 @@

DEBUG = bool(strtobool(os.getenv("DEBUG", "false")))

TESTING = "test" in sys.argv

# https://docs.djangoproject.com/en/5.0/ref/settings/#std:setting-ALLOWED_HOSTS
allowed_hosts = os.getenv("ALLOWED_HOSTS", ".localhost,127.0.0.1,[::1]")
ALLOWED_HOSTS = list(map(str.strip, allowed_hosts.split(",")))

# Application definitions
INSTALLED_APPS = [
"pages.apps.PagesConfig",
"debug_toolbar",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
Expand All @@ -39,7 +41,6 @@
]

MIDDLEWARE = [
"debug_toolbar.middleware.DebugToolbarMiddleware",
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
Expand All @@ -50,6 +51,13 @@
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]

if not TESTING:
INSTALLED_APPS = [*INSTALLED_APPS, "debug_toolbar"]
MIDDLEWARE = [
"debug_toolbar.middleware.DebugToolbarMiddleware",
*MIDDLEWARE,
]

ROOT_URLCONF = "config.urls"

# Starting with Django 4.1+ we need to pick which template loaders to use
Expand Down
7 changes: 6 additions & 1 deletion src/config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""

from django.conf import settings
from django.contrib import admin
from django.urls import include
from django.urls import path
Expand All @@ -23,5 +24,9 @@
path("up/", include("up.urls")),
path("", include("pages.urls")),
path("admin/", admin.site.urls),
path("__debug__/", include("debug_toolbar.urls")),
]
if not settings.TESTING:
urlpatterns = [
*urlpatterns,
path("__debug__/", include("debug_toolbar.urls")),
]

0 comments on commit 02db3ec

Please sign in to comment.