KEMBAR78
# Django | Sentry for Python Sentry's [Django](https://www.djangoproject.com/) 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](https://sentry.io) 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](https://pypi.org/project/aiocontextvars/). ## [Install](https://docs.sentry.io/platforms/python/integrations/django.md#install) Install `sentry-sdk` from PyPI with the `django` extra: ```bash pip install "sentry-sdk[django]" ``` ## [Configure](https://docs.sentry.io/platforms/python/integrations/django.md#configure) To configure the Sentry SDK, initialize it in your `settings.py` file: In addition to capturing errors, you can monitor interactions between multiple services or applications by [enabling tracing](https://docs.sentry.io/concepts/key-terms/tracing.md). You can also collect and analyze performance profiles from real users with [profiling](https://docs.sentry.io/product/explore/profiling.md). Select which Sentry features you'd like to install in addition to Error Monitoring to get the corresponding installation and configuration instructions below. Error Monitoring\[ ]Tracing\[ ]Profiling `settings.py` ```python import sentry_sdk sentry_sdk.init( dsn="https://examplePublicKey@o0.ingest.sentry.io/0", # Add data like request headers and IP for users; # see https://docs.sentry.io/platforms/python/data-management/data-collected/ for more info send_default_pii=True, # performance # Set traces_sample_rate to 1.0 to capture 100% # of transactions for tracing. traces_sample_rate=1.0, # performance # profiling # To collect profiles for all profile sessions, # set `profile_session_sample_rate` to 1.0. profile_session_sample_rate=1.0, # Profiles will be automatically collected while # there is an active span. profile_lifecycle="trace", # profiling ) ``` ## [Verify](https://docs.sentry.io/platforms/python/integrations/django.md#verify) 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: ```python from django.urls import path def trigger_error(request): division_by_zero = 1 / 0 urlpatterns = [ path('sentry-debug/', trigger_error), # ... ] ``` ## [Behavior](https://docs.sentry.io/platforms/python/integrations/django.md#behavior) ##### uWSGI and Sentry SDK If you're using uWSGI, note that it doesn't support threads by default. This might lead to unexpected behavior when using the Sentry SDK, from features not working properly to uWSGI workers crashing. To enable threading support in uWSGI, make sure you have both `--enable-threads` and `--py-call-uwsgi-fork-hooks` on. ### [Issue Reporting](https://docs.sentry.io/platforms/python/integrations/django.md#issue-reporting) * 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](https://docs.sentry.io/platforms/python/integrations/logging.md) integration (this integration is enabled by default). ### [Tracing](https://docs.sentry.io/platforms/python/integrations/django.md#tracing) The following parts of your Django project are monitored: * Middleware stack * Signals * Database queries * Redis commands * Access to Django caches The parameter [traces\_sample\_rate](https://docs.sentry.io/platforms/python/configuration/options.md#traces-sample-rate) needs to be set when initializing the Sentry SDK for performance measurements to be recorded. ## [Options](https://docs.sentry.io/platforms/python/integrations/django.md#options) By adding `DjangoIntegration` explicitly to your `sentry_sdk.init()` call you can set options for `DjangoIntegration` to change its behavior: ```python import django.db.models.signals import sentry_sdk from sentry_sdk.integrations.django import DjangoIntegration sentry_sdk.init( # ... integrations=[ DjangoIntegration( transaction_style='url', middleware_spans=True, signals_spans=True, signals_denylist=[ django.db.models.signals.pre_init, django.db.models.signals.post_init, ], cache_spans=False, http_methods_to_capture=("GET",), ), ], ) ``` You can pass the following keyword arguments to `DjangoIntegration()`: * `transaction_style`: How to name transactions that show up in Sentry tracing. * `"/myproject/myview/"` if you set `transaction_style="url"`. * `"myproject.myview"` if you set `transaction_style="function_name"`. 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](https://docs.djangoproject.com/en/dev/topics/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`. * `http_methods_to_capture`: A tuple containing all the HTTP methods that should create a transaction in Sentry. The default is `("CONNECT", "DELETE", "GET", "PATCH", "POST", "PUT", "TRACE",)`. (Note that `OPTIONS` and `HEAD` are missing by default.) ##### Added in 2.15.0 The `http_methods_to_capture` option. ## [Supported Versions](https://docs.sentry.io/platforms/python/integrations/django.md#supported-versions) * Django 1.11+ * Python 3.6+ The versions above apply for the current major version of the Python SDK. If you're looking to use Sentry with older Python or framework versions, consider using an older major version of the SDK.