Learn about using Sentry with Django.

Sentry's Django integration adds support for the Django framework. It enables automatic reporting of errors and exceptions as well as tracing. In order to get started using the integration, you should have a Sentry account and a project set up.

If you're using Python 3.7, Django applications with channels 2.0 will be correctly instrumented. Older versions of Python will require the installation of aiocontextvars.

Install sentry-sdk from PyPI with the django extra:

pip install --upgrade 'sentry-sdk[django]'

To configure the Sentry SDK, initialize it in your file:
import sentry_sdk

    # Set traces_sample_rate to 1.0 to capture 100%
    # of transactions for tracing.
    # Set profiles_sample_rate to 1.0 to profile 100%
    # of sampled transactions.
    # We recommend adjusting this value in production.

The snippet below includes an intentional error that will be captured by Sentry when triggered. This will allow you to make sure that everything is working as soon as you set it up:

from django.urls import path

def trigger_error(request):
    division_by_zero = 1 / 0

urlpatterns = [
    path('sentry-debug/', trigger_error),
    # ...

  • If you use django.contrib.auth and you've set send_default_pii=True in your call to init, user data (such as current user id, email address, username) will be attached to error events.
  • Request data will be attached to all events: HTTP method, URL, headers, form data, JSON payloads. Sentry excludes raw bodies and multipart file uploads.
  • Logs emitted by any logger will be recorded as breadcrumbs by the Logging integration (this integration is enabled by default).

The following parts of your Django project are monitored:

  • Middleware stack
  • Signals
  • Database queries
  • Redis commands
  • Access to Django caches

The parameter enable_tracing needs to be set when initializing the Sentry SDK for performance measurements to be recorded.

By adding DjangoIntegration explicitly to your sentry_sdk.init() call you can set options for DjangoIntegration to change its behavior:

import django.db.models.signals

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

    # ...

You can pass the following keyword arguments to DjangoIntegration():

  • transaction_style:

    How to name transactions showing up in Sentry tracing.

    • "/myproject/myview/<foo>" if you set transaction_style="url".
    • "myproject.myview" if you set transaction_style="endpoint".

    The default is "url".

  • middleware_spans:

    Create spans and track performance of all middleware in your Django project. Set to False to disable.

    The default is True.

  • signals_spans:

    Create spans and track performance of all synchronous Django signals receiver functions in your Django project. Set to False to disable.

    The default is True.

  • signals_denylist:

    A list of signals to exclude from performance tracking. No spans will be created for these.

    The default is [].

  • cache_spans:

    Create spans and track performance of all read operations to configured caches. The spans also include information if the cache access was a hit or a miss. Set to True to enable.

    The default is False.

  • Django 1.11+
  • Python 3.6+

The versions above apply for Sentry Python SDK version 2.0+, which drops support for some legacy Python and framework versions. If you're looking to use Sentry with older Python or framework versions, consider using an SDK version from the 1.x major line of releases.

Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").