Host a Django project documentation autogenerated with Sphinx on Read the Docs -- Django specifics
Tags: django, sphinx, readthedocs
Published: 2024-05-27 11:14:15

**Introduction:**
After generating the documentation locally with Sphinx for my Django project, the next step is to deploy it so that it can be easily accessed and shared. [Read the Docs](https://about.readthedocs.com/?ref=readthedocs.com) is an excellent platform for this purpose. It hosts documentation for millions of open-source projects, providing a seamless integration with various version control systems like GitHub, GitLab, and Bitbucket.

Deploying your documentation on Read the Docs involves a [few straightforward steps](https://docs.readthedocs.io/en/stable/tutorial/index.html). First, you need to configure Django specific settings within the `conf.py` file of the Sphinx configuration, create a settings file for important Django settings and after connect your project repository to Read the Docs and configure it to recognize your Sphinx documentation setup. This includes specifying the location of your documentation files and any additional dependencies required to build the documentation. Once set up, Read the Docs will automatically build and host your documentation, making it accessible at a custom URL.

Using Read the Docs ensures that your documentation is always in sync with your project. It automatically rebuilds the documentation whenever changes are pushed to your repository, allowing for continuous integration. This setup not only enhances the accessibility of your documentation but also contributes to better project maintenance and collaboration.

In summary, by leveraging Sphinx for auto-documentation and deploying with Read the Docs, you can create and maintain high-quality documentation for your Django project with minimal effort. This approach not only saves time but also ensures that your documentation is always current and accessible to your users and contributors.

<hr>
## 1) Setting up the Sphinx configuration file to deploy on Read the Docs:


This `conf.py` file in Sphinx documentation is a configuration file that contains settings and options to control the behavior and appearance of the generated documentation. It includes paths to project directories, extensions to be used (such as autodoc for extracting docstrings), and project-specific information like the project name and version. This file is essential for customizing the documentation build process to suit the needs of the project.

**In the `conf.py` file for Sphinx, the lines of code which setting up the environment to integrate a Django project:**
- The line `sys.path.insert(0, os.path.abspath(".."))` adds the parent directory of the documentation to the system path, allowing Sphinx to locate the Django project modules. 
- The `os.environ["DJANGO_SETTINGS_MODULE"] = "docs.django_settings"` line specifies the settings module for the Django project, ensuring that Django knows which settings to use. 
- Finally, `django.setup()` initializes the Django environment, making the project's models and other components accessible for documentation generation. [Django documentation](https://docs.djangoproject.com/en/5.0/topics/settings/#calling-django-setup-is-required-for-standalone-django-usage)

```python
# docs/conf.py

# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

import os
import sys
import django

from datetime import date

sys.path.insert(0, os.path.abspath(".."))
os.environ["DJANGO_SETTINGS_MODULE"] = "docs.django_settings"
django.setup()

project = "My Project"
copyright = f"{date.today().year}, DoriDoro"
author = "DoriDoro"
release = "0.1"

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = ["sphinx.ext.autodoc", "sphinx.ext.viewcode"]

templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]


# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = "sphinx_rtd_theme"
html_static_path = ["_static"]
```


<hr>
## 2) Setting up the `django_settings.py` file to configure the Django project:

Creating the `docs/django_settings.py` file is essential for configuring Django to work seamlessly with Sphinx for autodocumentation. This file provides the minimal settings required for Django to initialize properly within the Sphinx environment.

- `SECRET_KEY`: The `SECRET_KEY` setting is mandatory for any Django project. In this context, it is given a placeholder value ('docs-super-secret') to satisfy Django's requirement without exposing any real secret key used in production.

- `INSTALLED_APPS`: The `INSTALLED_APPS` setting includes the list of applications that Django needs to recognize during the documentation build process. This ensures that Sphinx can correctly load and document your Django apps, avoiding warnings and errors. The list includes both default Django apps (like admin, auth, contenttypes) and custom apps specific to your project.

By defining these minimal settings, `docs/django_settings.py` allows Sphinx and Read the Docs to import and document your Django models, views, and other components, providing a smooth and error-free documentation generation process.

```python
# docs/django_settings.py

"""
Minimal file so Sphinx can work with Django for autodocumenting.

Location: /docs/django_settings.py
"""

# SECRET_KEY for the documentation
SECRET_KEY = 'docs-super-secret'

# INSTALLED_APPS with these apps is necessary for Sphinx to build without warnings & errors
# Depending on your package, the list of apps may be different
INSTALLED_APPS = [
    "oc_lettings_site.apps.OCLettingsSiteConfig",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    # custom apps:
    "core",
    "lettings",
    "profiles",
]
```

<hr>
## 3) Connect your Django project GitHub repository with Read the Docs:

Follow the [Read the docs](https://docs.readthedocs.io/en/stable/tutorial/index.html#first-steps) starting from the **First Steps** section. This tutorial is straightforward and reliable. It assumes you have already created an account on Read the Docs.

Use [this link](https://readthedocs.org/dashboard/import/) to import your GitHub repository where your Django project is stored.

Next, create a file named `.readthedocs.yaml` in the root directory of your Django project and include the following information:

```python
# .readthedocs.yaml

# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the OS, Python version and other tools you might need
build:
  os: ubuntu-22.04
  tools:
    python: "3.10"

python:
  install:
    - requirements: docs/docs_requirements.txt

# Build documentation in the "docs/" directory with Sphinx
sphinx:
  configuration: docs/conf.py

```

**Remarks:**

```python
python:
  install:
    - requirements: docs/docs_requirements.txt
```
The file `docs/docs_requirements.txt` specifies the path to the requirements and dependencies file needed for building the documentation, located within the `docs` directory.


After the import of your GitHub repository is successful, Read the Docs will begin the build process for your Django project.