DRF Spectacular Sidecar
drf-spectacular-sidecar is a Django application that serves self-contained distribution builds of Swagger UI and Redoc. It acts as an optional addition to `drf-spectacular` for environments without CDN access, but can also be used independently. The project is actively maintained, with versions updated monthly (around the 1st) by sourcing distribution files from npm via jsdelivr. The current version is 2026.4.1.
Warnings
- gotcha Content Security Policy (CSP) errors, especially with libraries like `django-csp`, can prevent Swagger UI or Redoc from displaying correctly. The sidecar mitigates issues with external asset loading, but you might still need to adjust `CSP_DEFAULT_SRC` for `unsafe-inline` scripts or consider `SpectacularSwaggerSplitView` as an alternative.
- breaking When using custom static file storage backends (e.g., `ManifestStaticFilesStorage`, Amazon S3 via `django-storages`), the sidecar may fail to correctly resolve static file paths, leading to broken UI or `collectstatic` errors. This often manifests as attempts to load from `/static/` instead of the configured storage.
- gotcha It is crucial to add `'drf_spectacular_sidecar'` to your `INSTALLED_APPS` in `settings.py`. Failing to do so will prevent Django from discovering and serving the necessary static files for Swagger UI and Redoc, resulting in blank or non-functional documentation pages.
- gotcha The `drf-spectacular-sidecar` package is intended to serve static UI files locally, not to integrate a documentation interface directly into the Django Admin. Misunderstanding this purpose can lead to incorrect expectations about its functionality.
Install
-
pip install drf-spectacular-sidecar
Imports
- drf_spectacular_sidecar
INSTALLED_APPS = [ # ... 'drf_spectacular_sidecar', # ... ]
Quickstart
# settings.py
INSTALLED_APPS = [
# ... your other apps
'drf_spectacular_sidecar',
'drf_spectacular', # Only if you are using drf-spectacular
# ...
]
# Only if you are using drf-spectacular and want to use the sidecar for UI assets
SPECTACULAR_SETTINGS = {
'SWAGGER_UI_DIST': 'SIDECAR',
'SWAGGER_UI_FAVICON_HREF': 'SIDECAR',
'REDOC_DIST': 'SIDECAR',
# ... other drf-spectacular settings
}