Django

Installation

The latest scout-apm package supports Django 3.2+.

Older Django Versions: Older versions of Django may be supported by previous versions of the scout-apm package.

AInstall the scout-apm package:

pip install scout-apm

BConfigure Scout in your settings.py file:

# settings.py
INSTALLED_APPS = [
    "scout_apm.django",  # should be listed first
    # ... other apps ...
]

# Scout settings
SCOUT_MONITOR = True
SCOUT_KEY = "[AVAILABLE IN THE SCOUT UI]"
SCOUT_NAME = "A FRIENDLY NAME FOR YOUR APP"

If you wish to configure Scout via environment variables, use SCOUT_MONITOR, SCOUT_NAME, and SCOUT_KEY instead of providing these settings in settings.py.

Heroku Customers: If you've installed Scout via the Heroku Addon, the provisioning process automatically sets SCOUT_MONITOR and SCOUT_KEY via config vars. Only SCOUT_NAME is additionally required.

CDeploy.

It takes approximately five minutes for your data to first appear within the Scout UI.

Middleware

Scout automatically inserts its middleware into your settings on Django startup in its AppConfig.ready().

It adds one at the very start of the middleware stack, and one at the end, allowing it to profile your middleware and views.

This normally works just fine.

However, if you need to customize the middleware order or prevent your settings being changed, you can include the Scout middleware classes in your settings yourself. Scout will detect this and not automatically insert its middleware.

If you do customize, your metrics will be affected. Anything included before the first middleware timing middleware will not be profiled by Scout at all (unless you add custom instrumentation). Anything included after the view middleware will be profiled as part of your view, rather than as middleware.

To add the middleware if you’re using new-style Django middleware in the MIDDLEWARE setting, which was added in Django 1.10:

# settings.py
MIDDLEWARE = [
    # ... any middleware to run first ...
    "scout_apm.django.middleware.MiddlewareTimingMiddleware",
    # ... your normal middleware stack ...
    "scout_apm.django.middleware.ViewTimingMiddleware",
    # ... any middleware to run last ...
]

To add the middleware if you’re using old-style Django middleware in the MIDDLEWARE_SETTINGS setting, which was removed in Django 2.0:

# settings.py
MIDDLEWARE_CLASSES = [
    # ... any middleware to run first ...
    "scout_apm.django.middleware.OldStyleMiddlewareTimingMiddleware",
    # ... your normal middleware stack ...
    "scout_apm.django.middleware.OldStyleViewMiddleware",
    # ... any middleware to run last ...
]