Monitoring helps you see how your Django app is working. Error tracking tells you when something goes wrong so you can fix it fast.
Monitoring and error tracking in 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.
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
)# Using Django's logging for error tracking LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console': { 'class': 'logging.StreamHandler', }, }, 'root': { 'handlers': ['console'], 'level': 'WARNING', }, }
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.
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),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.
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.