Django
The Django integration adds support for the Django Web Framework from Version 1.6 upwards.
Install
Install our Python SDK using pip:
pip install --upgrade sentry-sdkConfigure
To configure the SDK, initialize it with the Django integration in your settings.py file:
settings.pyimport sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
sentry_sdk.init(
dsn="https://examplePublicKey@o0.ingest.sentry.io/0",
integrations=[DjangoIntegration()],
traces_sample_rate=1.0, # We recommend lowering this in production
# If you wish to associate users to errors (assuming you are using
# django.contrib.auth) you may enable sending PII data.
send_default_pii=True
)Verify
Verify your Sentry installation by creating a route that triggers an error:
from django.urls import path
def trigger_error(request):
division_by_zero = 1 / 0
urlpatterns = [
path('sentry-debug/', trigger_error),
# ...
]Visiting this route will trigger an error that will be captured by Sentry.
A note on Django Channels
A Django application using Channels 2.0 will be correctly instrumented under Python 3.7. For older versions of Python you'll have to install aiocontextvars from PyPI or your application will not start.
If you experience memory leaks in your channels consumers while using the SDK, you need to wrap your entire application in Sentry's ASGI middleware.
Unfortunately the SDK is not able to do so by itself, as Channels is missing some hooks for instrumentation.
Behavior
All exceptions leading to an Internal Server Error are reported.
Request data is attached to all events: HTTP method, URL, headers, form data, JSON payloads. Sentry excludes raw bodies and multipart file uploads. Sentry also excludes personally identifiable information (such as user ids, usernames, cookies, authorization headers, IP addresses) unless you set
send_default_piitoTrue.Each request has a separate scope. Changes to the scope within a view, for example setting a tag, will only apply to events sent as part of the request being handled.
If you use
django.contrib.authand have setsend_default_pii=Truein your call toinit, user data (current user id, email address, username) is attached to the event.The Sentry Python SDK will attach SQL queries as breadcrumbs.
Logging with any logger will create breadcrumbs when the Logging integration is enabled (done by default).
Options
You can pass the following keyword arguments to DjangoIntegration():
transaction_style:Copieddef my_function(request): return "ok" urlpatterns = [ url(r"^myurl/(?P<myid>\d+)/$", my_function) ]In the above code, you would set the transaction to:
/myurl/{myid}if you settransaction_style="url". This matches the behavior of the old Raven SDK.my_functionif you settransaction_style="function_name"
The default is
"url".
Reporting other status codes
In some situations, it might make sense to report 404 Not Found and other errors besides uncaught exceptions (500 Internal Server Error) to Sentry. You can achieve this by writing your own Django view for those status codes. For example:
# urls.py
handler404 = 'mysite.views.my_custom_page_not_found_view'
# views.py
from django.http import HttpResponseNotFound
from sentry_sdk import capture_message
def my_custom_page_not_found_view(*args, **kwargs):
capture_message("Page not found!", level="error")
# return any response here, e.g.:
return HttpResponseNotFound("Not found")The error message you send to Sentry will have the usual request data attached.
Refer to Customizing Error Views from the Django documentation for more information.
- Package:
- pypi:sentry-sdk
- Version:
- 0.18.0
- Repository:
- https://github.com/getsentry/sentry-python