Use Sentry for Error Tracking

A snippet showing how to integrate Sentry with Django.


# Install Sentry SDK
pip install --upgrade sentry-sdk
      

# settings.py

import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

sentry_sdk.init(
    dsn="https://@o.ingest.sentry.io/",
    integrations=[DjangoIntegration()],
    traces_sample_rate=0.1,  # adjust for performance tracing
    send_default_pii=False,  # user data/PII handling
)
      

# example in a view
from django.views.generic import View
import logging
logger = logging.getLogger(__name__)

class MyView(View):
    def get(self, request):
        try:
          1 / 0
        except Exception:
          logger.exception("Division by zero occurred")  # Sentry captures this
        return HttpResponse("Hello, world!")
      
Explanation:
  • Configure environment, release, and sampling to match your deployment.
  • Unhandled exceptions and logged errors are captured in Sentry with stacktraces and context.
  • Category Logging
  • Total Views 298
  • Last Modified 15 July, 2026
  • Tags #logging #sentry #error tracking #monitoring
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.