We place a high importance on 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:
This section explains how writers can craft their documentation changes in the most useful and least error-prone ways.
Though Django’s documentation is intended to be read as HTML at https://docs.djangoproject.com/, we edit it as a collection of text files for maximum flexibility. These files live in the top-level docs/ directory of a Django release.
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). The development version has the latest-and-greatest documentation, just as it has latest-and-greatest code. We also backport documentation fixes and improvements, at the discretion of the committer, to the last release branch. That’s because it’s highly 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.
To actually build the documentation locally, you’ll currently need to install Sphinx – sudo pip install Sphinx should do the trick.
Note
Building the Django documentation requires Sphinx 1.0.2 or newer. Sphinx also requires the Pygments library for syntax highlighting; building the Django documentation requires Pygments 1.1 or newer (a new-enough version should automatically be installed along with Sphinx).
Then, building the HTML is easy; just make html (or make.bat html on Windows) from the docs directory.
To get started contributing, you’ll want to read the reStructuredText Primer. After that, you’ll want to read about the Sphinx-specific markup that’s used to manage metadata, indexing, and cross-references.
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:
Here are some style guidelines on commonly used terms throughout the documentation:
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. There’s basically no limit to the amount of useful markup you can add.
Use intersphinx to reference Python’s and Sphinx’ documentation.
Besides the Sphinx built-in markup, Django’s docs defines 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 :djadminopt:`--traceback`.
Links to Trac tickets (typically reserved for minor release notes):
:ticket:`12345`
Our policy for new features is:
All documentation of new features should be written in a way that clearly designates the features are only available in the Django development version. Assume documentation readers are using the latest release, not the development version.
Our preferred way for marking new features is by prefacing the features’ documentation with: “.. versionadded:: X.Y”, followed by a mandatory blank line and an optional content (indented).
General improvements, or other changes to the APIs that should be emphasized should use the “.. versionchanged:: X.Y” directive (with the same format as the versionadded mentioned above.
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 tuple)
A tuple that lists people who get code error notifications. When
``DEBUG=False`` and a view raises an exception, Django will email these people
with the full exception information. Each member of the tuple should be a tuple
of (Full name, email address). Example::
(('John', 'john@example.com'), ('Mary', 'mary@example.com'))
Note that Django will email *all* of these people whenever an error happens.
See :doc:`/howto/error-reporting` for more information.
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.
A few small improvements can be made to make the documentation read and look better:
Most of the various index.txt documents have very short or even non-existent intro text. Each of those documents needs a good short intro the content below that point.
The glossary is very perfunctory. It needs to be filled out.
Add more metadata targets. Lots of places look like:
``File.close()``
~~~~~~~~~~~~~~~~
... these should be:
.. method:: File.close()
That is, use metadata instead of titles.
Add more links – nearly everything that’s an inline code literal right now can probably be turned into a xref.
See the literals_to_xrefs.py file in _ext – it’s a shell script to help do this work.
This will probably be a continuing, never-ending project.
Whenever possible, use links. So, use :setting:`ADMINS` instead of ``ADMINS``.
Use directives where appropriate. Some directives (e.g. .. setting::) are prefix-style directives; they go before the unit they’re describing. These are known as “crossref” directives. Others (e.g. .. class::) generate their own markup; these should go inside the section they’re describing. These are called “description units”.
You can tell which are which by looking at in _ext/djangodocs.py; it registers roles as one of the other.
Add .. code-block:: <lang> to literal blocks so that they get highlighted.
When referring to classes/functions/modules, etc., you’ll want to use the fully-qualified name of the target (:class:`django.contrib.contenttypes.models.ContentType`).
Since this doesn’t look all that awesome in the output – it shows the entire path to the object – you can prefix the target with a ~ (that’s a tilde) to get just the “last bit” of that path. So :class:`~django.contrib.contenttypes.models.ContentType` will just display a link with the title “ContentType”.
Before you commit your docs, it’s a good idea to run the spelling checker. You’ll need to install a couple packages first:
Then from the docs directory, run make 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:
See Localizing the Django documentation if you’d like to help translate the documentation into another language.
Dec 16, 2014