Skip to main content
Course/Module 6/Topic 5 of 5Advanced

Monitoring & Maintenance

Learn how to monitor and maintain Django applications in production for reliability and performance.

60 minBy Priygop TeamLast updated: Feb 2026

Production Monitoring

Monitoring and maintaining Django applications in production is essential for ensuring reliability and performance.

Monitoring Tools & Practices

Example
# Django Debug Toolbar (for development)
INSTALLED_APPS = [
    # ...
    'debug_toolbar',
]

MIDDLEWARE = [
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    # ... other middleware
]

INTERNAL_IPS = [
    '127.0.0.1',
]

# Custom management commands
# management/commands/check_site_health.py
from django.core.management.base import BaseCommand
from django.core.cache import cache
from django.db import connection

class Command(BaseCommand):
    help = 'Check site health and performance'

    def handle(self, *args, **options):
        # Check database connection
        try:
            with connection.cursor() as cursor:
                cursor.execute("SELECT 1")
            self.stdout.write(self.style.SUCCESS('Database: OK'))
        except Exception as e:
            self.stdout.write(self.style.ERROR(f'Database: ERROR - {e}'))

        # Check cache
        try:
            cache.set('health_check', 'ok', 10)
            if cache.get('health_check') == 'ok':
                self.stdout.write(self.style.SUCCESS('Cache: OK'))
            else:
                self.stdout.write(self.style.ERROR('Cache: ERROR'))
        except Exception as e:
            self.stdout.write(self.style.ERROR(f'Cache: ERROR - {e}'))

# Error tracking with Sentry
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration

sentry_sdk.init(
    dsn="your-sentry-dsn",
    integrations=[DjangoIntegration()],
    traces_sample_rate=1.0,
    send_default_pii=True
)

# Custom logging
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
            'style': '{',
        },
    },
    'handlers': {
        'file': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': '/var/log/django/app.log',
            'formatter': 'verbose',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file', 'console'],
            'level': 'INFO',
            'propagate': True,
        },
        'myapp': {
            'handlers': ['file', 'console'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

# Performance monitoring
from django.core.cache import cache
import time

def performance_monitor(func):
    def wrapper(*args, **kwargs):
        start_time = time.time()
        result = func(*args, **kwargs)
        end_time = time.time()

        execution_time = end_time - start_time
        cache_key = f'performance_{func.__name__}'
        
        # Store performance metrics
        cache.set(cache_key, execution_time, 3600)

        return result
    return wrapper

# Database backup
# management/commands/backup_db.py
from django.core.management.base import BaseCommand
from django.conf import settings
import subprocess
import os

class Command(BaseCommand):
    help = 'Backup database'

    def handle(self, *args, **options):
        db_settings = settings.DATABASES['default']

        if db_settings['ENGINE'] == 'django.db.backends.postgresql':
            # PostgreSQL backup
            backup_file = f'backup_{timezone.now().strftime("%Y%m%d_%H%M%S")}.sql'
            subprocess.run([
                'pg_dump',
                '-h', db_settings['HOST'],
                '-U', db_settings['USER'],
                '-d', db_settings['NAME'],
                '-f', backup_file
            ])
            self.stdout.write(self.style.SUCCESS(f'Backup created: {backup_file}'))

# Health check endpoint
from django.http import JsonResponse
from django.views.decorators.http import require_http_methods

@require_http_methods(["GET"])
def health_check(request):
    try:
        # Check database
        from django.db import connection
        with connection.cursor() as cursor:
            cursor.execute("SELECT 1")
        
        # Check cache
        cache.set('health_check', 'ok', 10)
        cache_ok = cache.get('health_check') == 'ok'

        if cache_ok:
            return JsonResponse({
                'status': 'healthy',
                'database': 'ok',
                'cache': 'ok'
            })
        else:
            return JsonResponse({
                'status': 'unhealthy',
                'cache': 'error'
            }, status=500)
            
    except Exception as e:
        return JsonResponse({
            'status': 'unhealthy',
            'error': str(e)
        }, status=500)

Try It Yourself — Deployment & Best Practices

Try It Yourself — Deployment & Best PracticesHTML
HTML Editor
✓ ValidTab = 2 spaces
HTML|32 lines|1680 chars|✓ Valid syntax
UTF-8

Quick Quiz — Deployment & Best Practices

Additional Resources

Recommended Reading

  • Django Deployment Checklist
  • Django Performance Optimization Guide
  • Django Monitoring Best Practices

Online Resources

  • Heroku Django Deployment Tutorial
  • DigitalOcean Django Guide
  • AWS Elastic Beanstalk Documentation
Chat on WhatsApp
Priygop - Leading Professional Development Platform | Expert Courses & Interview Prep