Django
The Django integration adds support for the Django Web Framework from Version 1.6 upwards.
Install sentry-sdk
:
$ pip install --upgrade 'sentry-sdk==0.13.5'
To configure the SDK, initialize it with the Django integration in your settings.py
file:
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
integrations=[DjangoIntegration()]
)
You can easily 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_pii
toTrue
. -
If you use
django.contrib.auth
and have setsend_default_pii=True
in 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
:def 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_function
if you settransaction_style="function_name"
The default is
"url"
. -
User Feedback
You can use the user feedback feature with this integration. For more information see User Feedback.
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.