sphinxcontrib-django
sphinxcontrib-django is a Sphinx extension that significantly improves the documentation of Django applications. It enhances Sphinx's autodoc by automatically listing model and form fields as parameters, improving field representations, linking related fields, and hiding irrelevant runtime details. Additionally, it provides custom text roles for cross-referencing Django's settings, template tags, filters, and admin commands. The library is currently at version 2.5 and actively maintained.
Common errors
-
Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
cause Sphinx autodoc is trying to import Django models or forms before the Django settings have been loaded and the Django application registry initialized.fixIn your `conf.py`, ensure you have set `os.environ['DJANGO_SETTINGS_MODULE'] = 'your_project.settings'` and called `django.setup()` after adjusting `sys.path` to include your Django project. -
ModuleNotFoundError: No module named 'your_django_app'
cause Sphinx cannot find your Django application modules because the Python path (`sys.path`) does not include your project's root directory or the directory containing your apps.fixIn your `conf.py`, add the path to your Django project's root (or where your apps reside) to `sys.path`. For example: `sys.path.insert(0, os.path.abspath('../src'))` if your Django project is in `src` relative to `docs`. -
Sphinx generates empty documentation for Django models/forms.
cause The `sphinxcontrib-django` extension is either not correctly enabled, Django's environment is not initialized, or autodoc directives (`automodule`, `autoclass`, etc.) are missing or misconfigured in your `.rst` files.fixVerify that `'sphinxcontrib_django'` is in your `extensions` list in `conf.py`, ensure `django.setup()` is called, and check your `.rst` files for correct `autodoc` directives (e.g., `.. automodule:: myapp.models` and `.. autoclass:: myapp.models.MyModel`). Ensure `django_settings` is also correctly set in `conf.py`.
Warnings
- breaking When using Sphinx 9.x or later, the `autodoc_use_legacy_class_based = True` setting might be required in `conf.py` to ensure proper autodoc functionality with Django classes. Failure to do so can result in incomplete or incorrect documentation.
- gotcha Failure to correctly configure Django settings can lead to `ImproperlyConfigured` exceptions during the Sphinx build process, preventing documentation generation. This typically happens when Django's environment is not properly set up before autodoc attempts to import models or forms.
- deprecated Previous versions of `sphinxcontrib-django` (and underlying Django/Python versions) have dropped support for older Python and Django releases. Ensure your environment matches the library's requirements to avoid compatibility issues.
Install
-
pip install sphinxcontrib-django
Imports
- sphinxcontrib_django
extensions = ['sphinx.ext.autodoc', 'sphinxcontrib_django']
Quickstart
import os
import sys
import django
# Adjust this path to your Django project's root directory
sys.path.insert(0, os.path.abspath('../src'))
# Configure Django settings module
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'
# Initialize Django
django.setup()
# -- Project information -----------------------------------------------------
project = 'My Django Project'
copyright = '2026, Your Name'
author = 'Your Name'
release = '0.1'
# -- General configuration ---------------------------------------------------
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.intersphinx',
'sphinxcontrib_django',
]
intersphinx_mapping = {
'django': ('https://docs.djangoproject.com/en/stable/', 'https://docs.djangoproject.com/en/stable/_objects'),
'python': ('http://docs.python.org/', None),
}
# sphinxcontrib-django specific configuration
django_settings = 'myproject.settings'
# Required for Sphinx >= 9 with autodoc
# autodoc_use_legacy_class_based = True
# Optional: show database table names
django_show_db_tables = True