sphinxcontrib-django

2.5 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

To quickly set up `sphinxcontrib-django`, first install it via pip. Then, create your Sphinx documentation project using `sphinx-quickstart`. Edit your `conf.py` file to include `sphinxcontrib_django` in the `extensions` list, configure the path to your Django project, set the `DJANGO_SETTINGS_MODULE` environment variable, and call `django.setup()`. This ensures Sphinx can properly load and inspect your Django models and forms. Remember to adjust `sys.path.insert` and `django_settings` to match your project structure. For Sphinx versions 9 and above, you might need to enable `autodoc_use_legacy_class_based`.

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

view raw JSON →