Sentry's Django integration adds support for the Django Framework. It enables automatic reporting of errors and exceptions as well as performance monitoring. 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.
sentry-sdk from PyPI with the
pip install --upgrade 'sentry-sdk[django]'
To configure the Sentry SDK, initialize it in your
import sentry_sdk sentry_sdk.init( dsn="https://examplePublicKey@o0.ingest.sentry.io/0", # Set traces_sample_rate to 1.0 to capture 100% # of transactions for performance monitoring. # We recommend adjusting this value in production, traces_sample_rate=1.0, )
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.authand you've set
send_default_pii=Truein 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
- Database queries
- Redis commands
traces_sample_rate needs to be set when initializing the Sentry SDK for performance measurements to be recorded.
DjangoIntegration explictly to your
sentry_sdk.init() call you can set options for
DjangoIntegration to change its behavior:
import sentry_sdk from sentry_sdk.integrations.django import DjangoIntegration sentry_sdk.init( dsn="https://examplePublicKey@o0.ingest.sentry.io/0", # ... integrations=[ DjangoIntegration( transaction_style='url', middleware_spans=True, signals_spans=False, ), ], )
You can pass the following keyword arguments to
How to name transactions showing up in Sentry performance monitoring.
"/myproject/myview/<foo>"if you set
"myproject.myview"if you set
The default is
Create spans and track performance of all middleware in your Django project. Set to
The default is
Create spans and track performance of all Django signals receiver functions in your Django project. Set to
The default is
- Django 1.8-1.11 (Python: 2.7+)
- Django 2.x (Python: 3.5+)
- Django 3.x (Python: 3.6+)
- Django 4.x (Python: 3.8+)
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").