Configure Contrast Django middleware for the Python agent

Django middleware is configured in the settings.py file. This file isn't found in the same location for all applications, but it's generally near the top of the application source tree. Common locations include:

  • /settings.py

  • config/settings.py

  • app/settings.py

Note

When searching the source tree to find the settings.py, make sure to exclude any directories that correspond to Python virtual environments.

Some applications have multiple settings.py files, which may correspond to different configurations of the application (for example, prod or test). In these cases, add the Contrast agent middleware to any and all of the configurations where it will be used.

For Django 1.10 and later and 2.0 and later, look for the MIDDLEWARE configuration variable, which is an array. Add the Contrast agent module to the list:

MIDDLEWARE = [
  'contrast.agent.middlewares.django_middleware.DjangoMiddleware',
  # OTHER MIDDLEWARE,
]

Include the Contrast middleware as early in the list as is possible; although modifying the order may be necessary to get the application working in some circumstances.

Older versions of Django have a different architecture for middlewares. For Django 1.6 to 1.9, look for the MIDDLEWARE_CLASSES configuration variable in settings.py.

MIDDLEWARE_CLASSES = [
  'contrast.agent.middlewares.legacy_django_middleware.DjangoMiddleware',
  # OTHER MIDDLEWARE
]

See the Django documentation for more details on middleware inclusion.