0
0
Djangoframework~5 mins

Monitoring and error tracking in Django

Choose your learning style9 modes available
Introduction

Monitoring helps you see how your Django app is working. Error tracking tells you when something goes wrong so you can fix it fast.

You want to know if your Django website is slow or down.
You want to get alerts when users face errors on your site.
You want to find and fix bugs before users complain.
You want to understand how users interact with your app.
You want to keep your app running smoothly all the time.
Syntax
Django
INSTALLED_APPS = [
    ...
    # Add monitoring tools or error tracking apps here
]

# Example: Configure Sentry for error tracking
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

sentry_sdk.init(
    dsn='your_sentry_dsn_here',
    integrations=[DjangoIntegration()],
    traces_sample_rate=1.0,
    send_default_pii=True
)

Put monitoring or error tracking apps in INSTALLED_APPS and middleware if needed.

Configure external services like Sentry with their SDK and your project DSN.

Examples
This example shows how to add Sentry to your Django app for error tracking with 50% performance tracing.
Django
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
]

import 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=0.5
)
This example configures Django's built-in logging to show warnings and errors in the console.
Django
# Using Django's logging for error tracking
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'WARNING',
    },
}
Sample Program

This Django settings.py setup adds Sentry for error tracking. Add the trigger_error view to your views.py and map it in urls.py (e.g., path('trigger-error/', trigger_error)) to test if Sentry catches the division by zero error.

Django
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

import 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,
    send_default_pii=True
)

# To test, add this view to views.py:
# from django.http import HttpResponse
#
# def trigger_error(request):
#     division_by_zero = 1 / 0
#     return HttpResponse("This won't be reached")
#
# And in urls.py: path('trigger-error/', trigger_error),
OutputSuccess
Important Notes

Always keep your monitoring and error tracking keys secret.

Test your error tracking by causing a safe error in a development environment.

Use monitoring dashboards to watch app health and respond quickly.

Summary

Monitoring shows how your Django app performs in real time.

Error tracking helps find and fix bugs fast.

Tools like Sentry integrate easily with Django for these tasks.