In any Django application, errors and performance issues are inevitable. Debugging issues in production can be challenging, especially when relying solely on logs and user reports. This is where Sentry comes in.
Sentry provides real-time error tracking and performance monitoring, helping developers quickly detect, diagnose, and fix issues before they impact users.
With just a few lines of code, Sentry seamlessly integrates with Django, providing deep insights into your application’s health and helping you deliver a more reliable experience for users.
DjangoIntegration
?Captures unhandled exceptions in Django views and middleware.
Automatically includes request details, user context, and session data.
Improves debugging for Django management commands and Celery tasks.
For a basic Django project, these settings should be sufficient, but Sentry offers additional customization options for more complex applications.
pip install --upgrade 'sentry-sdk[django]'
If you plan to use the DjangoIntegration option, this is the only package you need. Also, remember to add sentry-sdk
to your requirements.txt
file to ensure it remains in your dependencies.
Log in or sign up for a free account on Sentry.
Navigate to the Projects page and click Create Project.
Select Django as your platform, give your project a name, and confirm by clicking Create Project.
Sentry will provide setup instructions, including your DSN (Data Source Name), which you’ll use in your Django project.
In your newly created Sentry project, you’ll find documentation to help integrate sentry-sdk into your project.
First, import Sentry at the top of your settings.py
file and add the following configuration:
# blog_project/settings.py
import sentry_sdk
sentry_sdk.init(
dsn="https://yourkeygoeshere.ingest.sentry.io/project-number",
traces_sample_rate=1.0,
_experiments={
"continuous_profiling_auto_start": True,
},
)
To verify that Sentry is correctly integrated, create a URL endpoint that triggers an intentional error:
# core/urls.py
from django.urls import path
app_name = "core"
def trigger_error(request):
division_by_zero = 1 / 0
urlpatterns = [
path("sentry-debug/", trigger_error),
]
# blog_project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("blog/", include("blog.urls", namespace="blog")),
path("", include("core.urls", namespace="core")),
]
Start your development server:
python manage.py runserver
Then, navigate to:
http://127.0.0.1:8000/sentry-debug/
This will trigger a ZeroDivisionError, which Sentry should capture. Wait a minute or two, then refresh your Sentry dashboard to confirm that the error has been recorded.
DjangoIntegration
:DjangoIntegration
makes debugging and monitoring Django applications easier by automatically capturing request details, user information, and more.
To enable it, update your settings.py
with the following:
# blog_project/settings.py
from sentry_sdk.integrations.django import DjangoIntegration
sentry_sdk.init(
dsn="your-sentry-dsn",
integrations=[
DjangoIntegration(),
],
traces_sample_rate=1.0, # Enables performance monitoring
send_default_pii=True, # Sends user details and request data
)
By integrating Sentry into your Django project, you ensure better error tracking, faster debugging, and a more stable production environment.