Writing documentation#

We place high importance on the consistency and readability of documentation. After all, Django was created in a journalism environment! So we treat our documentation like we treat our code: we aim to improve it as often as possible.

Documentation changes generally come in two forms:

  • General improvements: typo corrections, error fixes and better explanations through clearer writing and more examples.

  • New features: documentation of features that have been added to the framework since the last release.

This section explains how writers can craft their documentation changes in the most useful and least error-prone ways.

The Django documentation process#

Though Django’s documentation is intended to be read as HTML at https://docs.djangoproject.com/, we edit it as a collection of plain text files written in the reStructuredText markup language for maximum flexibility.

We work from the development version of the repository because it has the latest-and-greatest documentation, just as it has the latest-and-greatest code.

We also backport documentation fixes and improvements, at the discretion of the merger, to the last release branch. This is because it’s advantageous to have the docs for the last release be up-to-date and correct (see Differences between versions).

Django’s documentation uses the Sphinx documentation system, which in turn is based on docutils. The basic idea is that lightly-formatted plain-text documentation is transformed into HTML, PDF, and any other output format.

Sphinx includes a sphinx-build command for turning reStructuredText into other formats, e.g., HTML and PDF. This command is configurable, but the Django documentation includes a Makefile that provides a shorter make html command.

How the documentation is organized#

The documentation is organized into several categories:

  • Tutorials take the reader by the hand through a series of steps to create something.

    The important thing in a tutorial is to help the reader achieve something useful, preferably as early as possible, in order to give them confidence.

    Explain the nature of the problem we’re solving, so that the reader understands what we’re trying to achieve. Don’t feel that you need to begin with explanations of how things work - what matters is what the reader does, not what you explain. It can be helpful to refer back to what you’ve done and explain afterward.

  • Topic guides aim to explain a concept or subject at a fairly high level.

    Link to reference material rather than repeat it. Use examples and don’t be reluctant to explain things that seem very basic to you - it might be the explanation someone else needs.

    Providing background context helps a newcomer connect the topic to things that they already know.

  • Reference guides contain technical references for APIs. They describe the functioning of Django’s internal machinery and instruct in its use.

    Keep reference material tightly focused on the subject. Assume that the reader already understands the basic concepts involved but needs to know or be reminded of how Django does it.

    Reference guides aren’t the place for general explanation. If you find yourself explaining basic concepts, you may want to move that material to a topic guide.

  • How-to guides are recipes that take the reader through steps in key subjects.

    What matters most in a how-to guide is what a user wants to achieve. A how-to should always be result-oriented rather than focused on internal details of how Django implements whatever is being discussed.

    These guides are more advanced than tutorials and assume some knowledge about how Django works. Assume that the reader has followed the tutorials and don’t hesitate to refer the reader back to the appropriate tutorial rather than repeat the same material.

How to start contributing documentation#

Clone the Django repository to your local machine#

If you’d like to start contributing to our docs, get the development version of Django from the source code repository (see Installing the development version):

$ git clone https://github.com/django/django.git
...\> git clone https://github.com/django/django.git

If you’re planning to submit these changes, you might find it useful to make a fork of the Django repository and clone this fork instead.

Set up a virtual environment and install dependencies#

Create and activate a virtual environment, then install the dependencies:

$ python -m venv .venv
$ source .venv/bin/activate
$ python -m pip install -r docs/requirements.txt

Build the documentation locally#

We can build HTML output from the docs directory:

$ cd docs
$ make html
...\> cd docs
...\> make.bat html

Your locally-built documentation will be accessible at _build/html/index.html and it can be viewed in any web browser, though it will be themed differently than the documentation at docs.djangoproject.com. This is OK! If your changes look good on your local machine, they’ll look good on the website.

Automating documentation rebuilds#

sphinx-autobuild can be used to automatically rebuild the documentation and reload the documentation page in the browser whenever a file changes. To enable auto-reloading:

  1. Install the package:

    $ python -m pip install sphinx-autobuild
    
    ...\> py -m pip install sphinx-autobuild
    
  2. From the docs directory, run one of the following commands:

    • On Linux and macOS:

      $ SPHINXBUILD=sphinx-autobuild SPHINXOPTS="--open-browser --delay 0" make html
      
    • On Windows (Command Prompt):

      ...\> set SPHINXBUILD=sphinx-autobuild
      ...\> set SPHINXOPTS=--open-browser --delay 0
      ...\> make html
      
    • On Windows (PowerShell):

      PS> $env:SPHINXBUILD="sphinx-autobuild"
      PS> $env:SPHINXOPTS="--open-browser --delay 0"
      PS> make html
      

    Alternatively, sphinx-autobuild can be invoked directly:

    $ sphinx-autobuild . _build/html --open-browser --delay 0
    
    ...\> sphinx-autobuild . _build\html --open-browser --delay 0
    

The auto-reloader can be stopped with Ctrl+C.

Making edits to the documentation#

The source files are .txt files located in the docs/ directory.

These files are written in the reStructuredText markup language. To learn the markup, see the reStructuredText reference.

To edit this page, for example, we would edit the file docs/internals/contributing/writing-documentation.txt and rebuild the HTML with make html.

Documentation quality checks#

Several checks help maintain Django’s documentation quality, including spelling, code block formatting, and documentation style.

These checks are run automatically in CI and must pass before documentation changes can be merged. They can also be run locally with a single command:

$ make check
...\> make.bat check

This command runs all current checks and will include any new checks added in the future, with the exception of EPUBCheck, which requires a Java runtime.

Spelling check#

Before you commit your docs, it’s a good idea to run the spelling checker. You’ll need to install sphinxcontrib-spelling first. The spell checker also requires a system-level spell checking backend such as Aspell. Then from the docs directory, run:

$ make spelling
...\> make.bat spelling

Wrong words (if any) along with the file and line number where they occur will be saved to _build/spelling/output.txt.

If you encounter false-positives (error output that actually is correct), do one of the following:

  • Surround inline code or brand/technology names with double grave accents (``)

  • Find synonyms that the spell checker recognizes.

  • If, and only if, you are sure the word you are using is correct - add it to docs/spelling_wordlist (please keep the list in alphabetical order).

Code block format check#

All Python code blocks should be formatted using the blacken-docs auto-formatter. This is automatically run by the pre-commit hook if configured.

The check can also be run manually: provided that blacken-docs and Sphinx are installed, run the following command from the docs directory:

$ make black
...\> make.bat black

The formatter will report any issues by printing them to the terminal and will reformat code blocks where possible.

Documentation lint check#

Django’s documentation is checked for reStructuredText style and formal issues using sphinx-lint. This helps catch problems like stray tabs, trailing whitespace, excessive line length, and similar formatting problems.

Once sphinx-lint and Sphinx are installed, the check can be run with the following command from the docs directory:

$ make lint
...\> make.bat lint

The command prints any violations to the terminal in the form path:line: message. If problems are encountered:

  • Read the message and fix the indicated issue (for example, remove trailing whitespace, adjust backticks, or replace tabs with spaces).

  • For long lines consider wrapping text onto new lines or breaking long inline links into named references. The custom line length check should already skip common false positives such as headings, tables and long links.

EPUBCheck#

Django’s documentation is also available in EPUB format. EPUB is based on the HTML build but has several limitations that don’t apply to content meant for web browsers. Verifying EPUB compatibility is particularly important when changing CSS files, images, or other assets that are included in the HTML documentation, or when altering any Sphinx toctree.

To ensure that the EPUB is valid, build it and check it with the EPUBCheck tool from the docs directory:

$ make epubcheck
...\> make.bat epubcheck

Some NAV-011 warnings are expected: they come from index pages with their own sections following a toctree, which makes the table of contents link back to a document earlier in the EPUB reading order. Any errors, however, indicate a problem that should be fixed.

The EPUBCheck tool requires Java, so it is not included in the standard set of quality checks run by make check.

Running sphinx-autobuild to automatically rebuild changes in HTML can interfere with EPUB builds. Sphinx warnings about .buildinfo.bak indicate overlapping builds that will lead to EPUBCheck errors. Stop sphinx-autobuild and run make clean if this occurs.

Voice#

We value the human voice in Django’s documentation, in which the reader can have the experience of one person addressing another.

The Django documentation is successful because it contains human rhythms and patterns. It speaks in the way an expert would speak to help a fellow developer. It expresses things in lively and imaginative ways. It’s systematic but not mechanical.

The documentation doesn’t erase the personality of its authors or smooth everything into a slippery, dull surface. Some friction is necessary for a reader to grasp a concept.

The documentation should not reach for grandiose but empty descriptions: for example, by overusing superlatives such as clear, robust, or structured.

To make this more concrete, we present the same information in two versions: human and frictionless. The “human” example is excerpted from this documentation. The “frictionless” version was produced by prompting an AI tool for a version “suitable for a pull request (smooth and professional)”:

Human version (excerpted from Django’s documentation)

How Django knows to UPDATE vs. INSERT

You may have noticed Django database objects use the same save() method for creating and changing objects. Django abstracts the need to use INSERT or UPDATE SQL statements. Specifically, when you call save() and the object’s primary key attribute does not define a default or db_default, Django follows this algorithm: …

The one gotcha here is that you should be careful not to specify a primary-key value explicitly when saving new objects, if you cannot guarantee the primary-key value is unused.

Frictionless version

How Django determines UPDATE vs. INSERT

Django model instances use a single method, save(), for both creating and updating records. Internally, Django determines whether to issue an INSERT or UPDATE SQL statement based on the state of the model instance and its primary key.

When calling save(), Django follows this general logic: …

One important caveat is to avoid explicitly assigning a primary key value when creating new objects unless you can guarantee that the value is unique. Providing a conflicting primary key may lead to unexpected behavior or database errors.

Notice how the revision does not address the reader (originally: “you may have noticed”). Notice the deleted figurative language (originally: “Django knows”) and a deleted technical term appropriate for the context (originally: “algorithm”). Notice how a self-evident point is belabored (revised: “may lead to unexpected behavior or database errors”). Finally, notice how the original version poses a problem (an abstraction is provided: how?), whereas the revised version merely summarizes (“Django determines … based on the state of …”), impairing the sense that the design was intentional.

Without a doubt, a different prompt would produce a different, perhaps improved result. Still, we include this example to illustrate how easily the documentation can be undermined when attention is not given to voice.

Writing style#

When using pronouns in reference to a hypothetical person, such as “a user with a session cookie”, gender-neutral pronouns (they/their/them) should be used. Instead of:

  • he or she… use they.

  • him or her… use them.

  • his or her… use their.

  • his or hers… use theirs.

  • himself or herself… use themselves.

Try to avoid using words that minimize the difficulty involved in a task or operation, such as “easily”, “simply”, “just”, “merely”, “straightforward”, and so on. People’s experience may not match your expectations, and they may become frustrated when they do not find a step as “straightforward” or “simple” as it is implied to be.

Commonly used terms#

Here are some style guidelines on commonly used terms throughout the documentation:

  • Django – when referring to the framework, capitalize Django. It is lowercase only in Python code and in the djangoproject.com logo.

  • email – no hyphen.

  • HTTP – the expected pronunciation is “Aitch Tee Tee Pee” and therefore should be preceded by “an” and not “a”.

  • MySQL, PostgreSQL, SQLite

  • SQL – when referring to SQL, the expected pronunciation should be “Ess Queue Ell” and not “sequel”. Thus in a phrase like “Returns an SQL expression”, “SQL” should be preceded by “an” and not “a”.

  • Python – when referring to the language, capitalize Python.

  • realize, customize, initialize, etc. – use the American “ize” suffix, not “ise.”

  • subclass – it’s a single word without a hyphen, both as a verb (“subclass that model”) and as a noun (“create a subclass”).

  • the web, web framework – it’s not capitalized.

  • website – use one word, without capitalization.

Django-specific terminology#

  • model – it’s not capitalized.

  • template – it’s not capitalized.

  • URLconf – use three capitalized letters, with no space before “conf.”

  • view – it’s not capitalized.

Guidelines for reStructuredText files#

These guidelines regulate the format of our reST (reStructuredText) documentation:

  • In section titles, capitalize only initial words and proper nouns.

  • Wrap the documentation at 80 characters wide, unless a code example is significantly less readable when split over two lines, or for another good reason.

  • The main thing to keep in mind as you write and edit docs is that the more semantic markup you can add the better. So:

    Add ``django.contrib.auth`` to your ``INSTALLED_APPS``...
    

    Isn’t nearly as helpful as:

    Add :mod:`django.contrib.auth` to your :setting:`INSTALLED_APPS`...
    

    This is because Sphinx will generate proper links for the latter, which greatly helps readers.

    You can prefix the target with a ~ (that’s a tilde) to get only the “last bit” of that path. So :mod:`~django.contrib.auth` will display a link with the title “auth”.

  • Use intersphinx to reference Python’s and Sphinx’ documentation.

  • Add .. code-block:: <lang> to literal blocks so that they get highlighted. Prefer relying on automatic highlighting using :: (two colons). This has the benefit that if the code contains some invalid syntax, it won’t be highlighted. Adding .. code-block:: python, for example, will force highlighting despite invalid syntax.

  • To improve readability, use .. admonition:: Descriptive title rather than .. note::. Use these boxes sparingly.

  • Use these heading styles:

    ===
    One
    ===
    
    Two
    ===
    
    Three
    -----
    
    Four
    ~~~~
    
    Five
    ^^^^
    
  • Use :rfc: to reference a Request for Comments (RFC) and try to link to the relevant section if possible. For example, use :rfc:`2324#section-2.3.2` or :rfc:`Custom link text <2324#section-2.3.2>`.

  • Use :pep: to reference a Python Enhancement Proposal (PEP) and try to link to the relevant section if possible. For example, use :pep:`20#easter-egg` or :pep:`Easter Egg <20#easter-egg>`.

  • Use :mimetype: to refer to a MIME Type unless the value is quoted for a code example.

  • Use :envvar: to refer to an environment variable. You may also need to define a reference to the documentation for that environment variable using .. envvar::.

  • Use :cve: to reference a Common Vulnerabilities and Exposures (CVE) identifier. For example, use :cve:`2019-14232`.

  • When documenting Python objects (classes, methods, attributes, etc.) using Sphinx directives such as .. class::, .. method::, and .. attribute::, all content must be properly indented to ensure correct rendering and to support features like automatic table of contents generation.

    Follow these rules:

    • The directive itself remains flush with the left margin (no indentation).

    • All descriptive text under the directive must be indented by 4 spaces.

    • Multi-line descriptions must keep the same indentation level.

    • Nested directives (for example, methods inside a class) require an additional 4 spaces of indentation to maintain hierarchy.

    • Field lists (such as :param:, :returns:, etc.) must align with the directive’s content level.

    Example:

    .. class:: MyClass
    
        A brief description of the class.
    
        .. method:: my_method(arg1, arg2)
    
            Method description.
    
            :param arg1: Description of the first parameter
            :param arg2: Description of the second parameter
    
        .. attribute:: my_attribute
    
            Attribute description.
    

Django-specific markup#

Besides Sphinx’s built-in markup, Django’s docs define some extra description units:

  • Settings:

    .. setting:: INSTALLED_APPS
    

    To link to a setting, use :setting:`INSTALLED_APPS`.

  • Template tags:

    .. templatetag:: regroup
    

    To link, use :ttag:`regroup`.

  • Template filters:

    .. templatefilter:: linebreaksbr
    

    To link, use :tfilter:`linebreaksbr`.

  • Field lookups (i.e. Foo.objects.filter(bar__exact=whatever)):

    .. fieldlookup:: exact
    

    To link, use :lookup:`exact`.

  • django-admin commands:

    .. django-admin:: migrate
    

    To link, use :djadmin:`migrate`.

  • django-admin command-line options:

    .. django-admin-option:: --traceback
    

    To link, use :option:`command_name --traceback` (or omit command_name for the options shared by all commands like --verbosity).

  • Links to Trac tickets (typically reserved for patch release notes):

    :ticket:`12345`
    

Django’s documentation uses a custom console directive for documenting command-line examples involving django-admin, manage.py, python, etc. In the HTML documentation, it renders a two-tab UI, with one tab showing a Unix-style command prompt and a second tab showing a Windows prompt.

For example, you can replace this fragment:

use this command:

.. code-block:: console

    $ python manage.py shell

with this one:

use this command:

.. console::

    $ python manage.py shell

Notice two things:

  • You usually will replace occurrences of the .. code-block:: console directive.

  • You don’t need to change the actual content of the code example. You still write it assuming a Unix-y environment (i.e. a '$' prompt symbol, '/' as filesystem path components separator, etc.)

The example above will render a code example block with two tabs. The first one will show:

$ python manage.py shell

(No changes from what .. code-block:: console would have rendered).

The second one will show:

...\> py manage.py shell

Documenting new features#

Our policy for new features is:

All documentation of new features should be written in a way that clearly designates the features that are only available in the Django development version. Assume documentation readers are using the latest release, not the development version.

New features and significant changes should be identified using Sphinx’s versionadded and versionchanged directives in their documentation, and should also be mentioned in the appropriate section of the release notes (docs/releases/A.B.txt).

Our preferred way to mark a new feature is to preface its documentation with “.. versionadded:: A.B”. This should usually be placed at the top of a section, just below the heading or Sphinx directive that introduces the new feature:

.. class:: Author(last_name, first_name)

    .. versionadded:: A.B

    A person who writes books...

If the new feature doesn’t have its own section, the versionadded annotation can be placed near where it is mentioned and should be followed by a blank line and an indented description of what was added:

Books
-----

Books are...

Each book can have one or more :class:`Author`\s...

.. versionadded:: A.B

    Support for multiple authors.

Sphinx renders this as “Added in version A.B: Support for multiple authors.” Avoid using “added” or similar words in the description when it would be redundant.

General improvements and significant changes to APIs should be marked with “.. versionchanged:: A.B” followed by a blank line and an indented description of the changes. Sphinx renders this as “Changed in version A.B:” followed by the description.

Put versionchanged notes at the bottom of a section. Related changes in the same release can be collected in a single note, split into multiple paragraphs if helpful for readability. Adjacent versionchanged directives covering different releases should be sorted from oldest to newest version.

These versionadded and versionchanged blocks should be “self-contained.” Since we only keep these annotations around for two releases, it’s nice to be able to remove the annotation and its contents without having to reflow, reindent, or edit the surrounding text. For example, instead of mentioning what’s new or changed in the same paragraphs that describe the feature, do something like this:

.. class:: Author(last_name, first_name=None)

    A person who writes books...

    ``last_name`` and ``first_name`` are...

    .. versionchanged:: A.B

        The ``first_name`` argument was made optional.

.. class:: Book(author)

When the time comes, we can delete the lines containing .. versionchanged:: A.B and its indented description without editing any other lines.

For similar reasons, avoid referring to a specific version of Django outside a versionadded or versionchanged block. Such references become outdated and are difficult to maintain. (Even inside one of those blocks, it’s often redundant to mention the version number in the description. Sphinx already includes it in the “Added in version A.B” or “Changed in version A.B” prefix.)

Minimizing images#

Optimize image compression where possible. For PNG files, use OptiPNG and AdvanceCOMP’s advpng:

$ cd docs
$ optipng -o7 -zm1-9 -i0 -strip all `find . -type f -not -path "./_build/*" -name "*.png"`
$ advpng -z4 `find . -type f -not -path "./_build/*" -name "*.png"`
...\> cd docs
...\> optipng -o7 -zm1-9 -i0 -strip all `find . -type f -not -path ".\_build\*" -name "*.png"`
...\> advpng -z4 `find . -type f -not -path ".\_build\*" -name "*.png"`

This is based on OptiPNG version 0.7.5. Older versions may complain about the -strip all option being lossy.

An example#

For a quick example of how it all fits together, consider this hypothetical example:

  • First, the ref/settings.txt document could have an overall layout like this:

    ========
    Settings
    ========
    
    ...
    
    .. _available-settings:
    
    Available settings
    ==================
    
    ...
    
    .. _deprecated-settings:
    
    Deprecated settings
    ===================
    
    ...
    
  • Next, the topics/settings.txt document could contain something like this:

    You can access a :ref:`listing of all available settings
    <available-settings>`. For a list of deprecated settings see
    :ref:`deprecated-settings`.
    
    You can find both in the :doc:`settings reference document
    </ref/settings>`.
    

    We use the Sphinx doc cross-reference element when we want to link to another document as a whole and the ref element when we want to link to an arbitrary location in a document.

  • Next, notice how the settings are annotated:

    .. setting:: ADMINS
    
    ADMINS
    ======
    
    Default: ``[]`` (Empty list)
    
    A list of all the people who get code error notifications...
    

    This marks up the following header as the “canonical” target for the setting ADMINS. This means any time I talk about ADMINS, I can reference it using :setting:`ADMINS`.

That’s basically how everything fits together.

Translating documentation#

See Localizing the Django documentation if you’d like to help translate the documentation into another language.

django-admin man page#

Sphinx can generate a manual page for the django-admin command. This is configured in docs/conf.py. Unlike other documentation output, this man page should be included in the Django repository and the releases as docs/man/django-admin.1. There isn’t a need to update this file when updating the documentation, as it’s updated once as part of the release process.

To generate an updated version of the man page, in the docs directory, run:

$ make man
...\> make.bat man

The new man page will be written in docs/_build/man/django-admin.1.