Django Silk
Django Silk is a live profiling and inspection tool for the Django framework, designed to help developers identify performance bottlenecks. It intercepts and stores HTTP requests, responses, and database queries, presenting them in an intuitive user interface for detailed analysis. Currently at version 5.5.0, it maintains an active release cadence, supporting recent Django and Python versions.
Warnings
- breaking Django Silk v5.3.0 removed support for Django 3.2 and Python 3.8. Ensure your environment meets the minimum requirements (Python >= 3.10, Django >= 4.2).
- deprecated The `SILKY_STORAGE_CLASS` setting was deprecated in version 5.1.0. Users should now leverage Django's standard `STORAGES` configuration for specifying storage backends.
- gotcha The order of middleware is sensitive. If `silk.middleware.SilkyMiddleware` is placed after another middleware that returns a response without calling `get_response`, Silk will not profile that request. If using `django.middleware.gzip.GZipMiddleware`, it must be placed *before* `SilkyMiddleware` to avoid encoding errors.
- gotcha Using Django Silk in production environments is generally not recommended due to performance overhead (intercepting and storing all request/response data), potential for significant database storage growth, and security implications (exposing application internals).
- gotcha There have been recurring fixes related to serialization issues for binary and JSON fields, as well as race conditions when modifying SQL execution, in versions 5.4.x. While fixed, this indicates potential instability in complex serialization scenarios.
- gotcha When using Python 3.12 or later, the built-in `cProfile` (used by Silk for Python profiling) cannot run concurrently. This means if another profile is already running, Silk's Python profiler will not execute.
Install
-
pip install django-silk -
pip install django-silk[formatting]
Imports
- silk
INSTALLED_APPS = [... 'silk', ...]
- SilkyMiddleware
MIDDLEWARE = [... 'silk.middleware.SilkyMiddleware', ...]
- silk.urls
from django.urls import path, include urlpatterns = [ path('silk/', include('silk.urls', namespace='silk')) ]
Quickstart
# settings.py
INSTALLED_APPS = [
# ... other apps
'silk',
]
MIDDLEWARE = [
# Place SilkyMiddleware near the top to capture full request cycle
# If using GZipMiddleware, place it *before* SilkyMiddleware.
# 'django.middleware.gzip.GZipMiddleware',
'silk.middleware.SilkyMiddleware',
# ... other middleware
]
# Optional: Restrict access to Silk UI
# SILKY_AUTHENTICATION = True # Requires user to be logged in
# SILKY_AUTHORISATION = True # Requires logged-in user to be staff
# LOGIN_URL = '/admin/login/' # Example for custom login URL if SILKY_AUTHENTICATION is True
# urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('silk/', include('silk.urls', namespace='silk')),
# ... other url patterns
]
# After configuration, run migrations:
# python manage.py migrate
# Access the UI at /silk/