1. =====================
    
  2. Writing documentation
    
  3. =====================
    
  4. 
    
  5. We place high importance on the consistency and readability of documentation.
    
  6. After all, Django was created in a journalism environment! So we treat our
    
  7. documentation like we treat our code: we aim to improve it as often as
    
  8. possible.
    
  9. 
    
  10. Documentation changes generally come in two forms:
    
  11. 
    
  12. * General improvements: typo corrections, error fixes and better
    
  13.   explanations through clearer writing and more examples.
    
  14. 
    
  15. * New features: documentation of features that have been added to the
    
  16.   framework since the last release.
    
  17. 
    
  18. This section explains how writers can craft their documentation changes
    
  19. in the most useful and least error-prone ways.
    
  20. 
    
  21. Getting the raw documentation
    
  22. =============================
    
  23. 
    
  24. Though Django's documentation is intended to be read as HTML at
    
  25. https://docs.djangoproject.com/, we edit it as a collection of text files for
    
  26. maximum flexibility. These files live in the top-level ``docs/`` directory of a
    
  27. Django release.
    
  28. 
    
  29. If you'd like to start contributing to our docs, get the development version of
    
  30. Django from the source code repository
    
  31. (see :ref:`installing-development-version`). The development version has the
    
  32. latest-and-greatest documentation, just as it has the latest-and-greatest code.
    
  33. We also backport documentation fixes and improvements, at the discretion of the
    
  34. merger, to the last release branch. That's because it's highly advantageous to
    
  35. have the docs for the last release be up-to-date and correct (see
    
  36. :ref:`differences-between-doc-versions`).
    
  37. 
    
  38. Getting started with Sphinx
    
  39. ===========================
    
  40. 
    
  41. Django's documentation uses the Sphinx__ documentation system, which in turn
    
  42. is based on docutils__. The basic idea is that lightly-formatted plain-text
    
  43. documentation is transformed into HTML, PDF, and any other output format.
    
  44. 
    
  45. __ https://www.sphinx-doc.org/
    
  46. __ https://docutils.sourceforge.io/
    
  47. 
    
  48. To build the documentation locally, install Sphinx:
    
  49. 
    
  50. .. console::
    
  51. 
    
  52.      $ python -m pip install Sphinx
    
  53. 
    
  54. Then from the ``docs`` directory, build the HTML:
    
  55. 
    
  56. .. console::
    
  57. 
    
  58.      $ make html
    
  59. 
    
  60. To get started contributing, you'll want to read the :ref:`reStructuredText
    
  61. reference <sphinx:rst-index>`.
    
  62. 
    
  63. Your locally-built documentation will be themed differently than the
    
  64. documentation at `docs.djangoproject.com <https://docs.djangoproject.com/>`_.
    
  65. This is OK! If your changes look good on your local machine, they'll look good
    
  66. on the website.
    
  67. 
    
  68. How the documentation is organized
    
  69. ==================================
    
  70. 
    
  71. The documentation is organized into several categories:
    
  72. 
    
  73. * :doc:`Tutorials </intro/index>` take the reader by the hand through a series
    
  74.   of steps to create something.
    
  75. 
    
  76.   The important thing in a tutorial is to help the reader achieve something
    
  77.   useful, preferably as early as possible, in order to give them confidence.
    
  78. 
    
  79.   Explain the nature of the problem we're solving, so that the reader
    
  80.   understands what we're trying to achieve. Don't feel that you need to begin
    
  81.   with explanations of how things work - what matters is what the reader does,
    
  82.   not what you explain. It can be helpful to refer back to what you've done and
    
  83.   explain afterward.
    
  84. 
    
  85. * :doc:`Topic guides </topics/index>` aim to explain a concept or subject at a
    
  86.   fairly high level.
    
  87. 
    
  88.   Link to reference material rather than repeat it. Use examples and don't be
    
  89.   reluctant to explain things that seem very basic to you - it might be the
    
  90.   explanation someone else needs.
    
  91. 
    
  92.   Providing background context helps a newcomer connect the topic to things
    
  93.   that they already know.
    
  94. 
    
  95. * :doc:`Reference guides </ref/index>` contain technical references for APIs.
    
  96.   They describe the functioning of Django's internal machinery and instruct in
    
  97.   its use.
    
  98. 
    
  99.   Keep reference material tightly focused on the subject. Assume that the
    
  100.   reader already understands the basic concepts involved but needs to know or
    
  101.   be reminded of how Django does it.
    
  102. 
    
  103.   Reference guides aren't the place for general explanation. If you find
    
  104.   yourself explaining basic concepts, you may want to move that material to a
    
  105.   topic guide.
    
  106. 
    
  107. * :doc:`How-to guides </howto/index>` are recipes that take the reader through
    
  108.   steps in key subjects.
    
  109. 
    
  110.   What matters most in a how-to guide is what a user wants to achieve.
    
  111.   A how-to should always be result-oriented rather than focused on internal
    
  112.   details of how Django implements whatever is being discussed.
    
  113. 
    
  114.   These guides are more advanced than tutorials and assume some knowledge about
    
  115.   how Django works. Assume that the reader has followed the tutorials and don't
    
  116.   hesitate to refer the reader back to the appropriate tutorial rather than
    
  117.   repeat the same material.
    
  118. 
    
  119. Writing style
    
  120. =============
    
  121. 
    
  122. When using pronouns in reference to a hypothetical person, such as "a user with
    
  123. a session cookie", gender-neutral pronouns (they/their/them) should be used.
    
  124. Instead of:
    
  125. 
    
  126. * he or she... use they.
    
  127. * him or her... use them.
    
  128. * his or her... use their.
    
  129. * his or hers... use theirs.
    
  130. * himself or herself... use themselves.
    
  131. 
    
  132. Try to avoid using words that minimize the difficulty involved in a task or
    
  133. operation, such as "easily", "simply", "just", "merely", "straightforward", and
    
  134. so on. People's experience may not match your expectations, and they may become
    
  135. frustrated when they do not find a step as "straightforward" or "simple" as it
    
  136. is implied to be.
    
  137. 
    
  138. Commonly used terms
    
  139. ===================
    
  140. 
    
  141. Here are some style guidelines on commonly used terms throughout the
    
  142. documentation:
    
  143. 
    
  144. * **Django** -- when referring to the framework, capitalize Django. It is
    
  145.   lowercase only in Python code and in the djangoproject.com logo.
    
  146. 
    
  147. * **email** -- no hyphen.
    
  148. 
    
  149. * **HTTP** -- the expected pronunciation is "Aitch Tee Tee Pee" and therefore
    
  150.   should be preceded by "an" and not "a".
    
  151. 
    
  152. * **MySQL**, **PostgreSQL**, **SQLite**
    
  153. 
    
  154. * **SQL** -- when referring to SQL, the expected pronunciation should be
    
  155.   "Ess Queue Ell" and not "sequel". Thus in a phrase like "Returns an
    
  156.   SQL expression", "SQL" should be preceded by "an" and not "a".
    
  157. 
    
  158. * **Python** -- when referring to the language, capitalize Python.
    
  159. 
    
  160. * **realize**, **customize**, **initialize**, etc. -- use the American
    
  161.   "ize" suffix, not "ise."
    
  162. 
    
  163. * **subclass** -- it's a single word without a hyphen, both as a verb
    
  164.   ("subclass that model") and as a noun ("create a subclass").
    
  165. 
    
  166. * **the web**, **web framework** -- it's not capitalized.
    
  167. 
    
  168. * **website** -- use one word, without capitalization.
    
  169. 
    
  170. Django-specific terminology
    
  171. ===========================
    
  172. 
    
  173. * **model** -- it's not capitalized.
    
  174. 
    
  175. * **template** -- it's not capitalized.
    
  176. 
    
  177. * **URLconf** -- use three capitalized letters, with no space before
    
  178.   "conf."
    
  179. 
    
  180. * **view** -- it's not capitalized.
    
  181. 
    
  182. Guidelines for reStructuredText files
    
  183. =====================================
    
  184. 
    
  185. These guidelines regulate the format of our reST (reStructuredText)
    
  186. documentation:
    
  187. 
    
  188. * In section titles, capitalize only initial words and proper nouns.
    
  189. 
    
  190. * Wrap the documentation at 80 characters wide, unless a code example
    
  191.   is significantly less readable when split over two lines, or for another
    
  192.   good reason.
    
  193. 
    
  194. * The main thing to keep in mind as you write and edit docs is that the
    
  195.   more semantic markup you can add the better. So::
    
  196. 
    
  197.       Add ``django.contrib.auth`` to your ``INSTALLED_APPS``...
    
  198. 
    
  199.   Isn't nearly as helpful as::
    
  200. 
    
  201.       Add :mod:`django.contrib.auth` to your :setting:`INSTALLED_APPS`...
    
  202. 
    
  203.   This is because Sphinx will generate proper links for the latter, which
    
  204.   greatly helps readers.
    
  205. 
    
  206.   You can prefix the target with a ``~`` (that's a tilde) to get only the
    
  207.   "last bit" of that path. So ``:mod:`~django.contrib.auth``` will
    
  208.   display a link with the title "auth".
    
  209. 
    
  210. * Use :mod:`~sphinx.ext.intersphinx` to reference Python's and Sphinx'
    
  211.   documentation.
    
  212. 
    
  213. * Add ``.. code-block:: <lang>`` to literal blocks so that they get
    
  214.   highlighted. Prefer relying on automatic highlighting using ``::``
    
  215.   (two colons). This has the benefit that if the code contains some invalid
    
  216.   syntax, it won't be highlighted. Adding ``.. code-block:: python``, for
    
  217.   example, will force highlighting despite invalid syntax.
    
  218. 
    
  219. * To improve readability, use ``.. admonition:: Descriptive title`` rather than
    
  220.   ``.. note::``. Use these boxes sparingly.
    
  221. 
    
  222. * Use these heading styles::
    
  223. 
    
  224.     ===
    
  225.     One
    
  226.     ===
    
  227. 
    
  228.     Two
    
  229.     ===
    
  230. 
    
  231.     Three
    
  232.     -----
    
  233. 
    
  234.     Four
    
  235.     ~~~~
    
  236. 
    
  237.     Five
    
  238.     ^^^^
    
  239. 
    
  240. * Use :rst:role:`:rfc:<rfc>` to reference RFC and try to link to the relevant
    
  241.   section if possible. For example, use ``:rfc:`2324#section-2.3.2``` or
    
  242.   ``:rfc:`Custom link text <2324#section-2.3.2>```.
    
  243. 
    
  244. * Use :rst:role:`:pep:<pep>` to reference a Python Enhancement Proposal (PEP)
    
  245.   and try to link to the relevant section if possible. For example, use
    
  246.   ``:pep:`20#easter-egg``` or ``:pep:`Easter Egg <20#easter-egg>```.
    
  247. 
    
  248. * Use :rst:role:`:mimetype:<mimetype>` to refer to a MIME Type unless the value
    
  249.   is quoted for a code example.
    
  250. 
    
  251. * Use :rst:role:`:envvar:<envvar>` to refer to an environment variable. You may
    
  252.   also need to define a reference to the documentation for that environment
    
  253.   variable using :rst:dir:`.. envvar:: <envvar>`.
    
  254. 
    
  255. Django-specific markup
    
  256. ======================
    
  257. 
    
  258. Besides :ref:`Sphinx's built-in markup <sphinx:rst-index>`, Django's docs
    
  259. define some extra description units:
    
  260. 
    
  261. * Settings::
    
  262. 
    
  263.         .. setting:: INSTALLED_APPS
    
  264. 
    
  265.   To link to a setting, use ``:setting:`INSTALLED_APPS```.
    
  266. 
    
  267. * Template tags::
    
  268. 
    
  269.         .. templatetag:: regroup
    
  270. 
    
  271.   To link, use ``:ttag:`regroup```.
    
  272. 
    
  273. * Template filters::
    
  274. 
    
  275.         .. templatefilter:: linebreaksbr
    
  276. 
    
  277.   To link, use ``:tfilter:`linebreaksbr```.
    
  278. 
    
  279. * Field lookups (i.e. ``Foo.objects.filter(bar__exact=whatever)``)::
    
  280. 
    
  281.         .. fieldlookup:: exact
    
  282. 
    
  283.   To link, use ``:lookup:`exact```.
    
  284. 
    
  285. * ``django-admin`` commands::
    
  286. 
    
  287.         .. django-admin:: migrate
    
  288. 
    
  289.   To link, use ``:djadmin:`migrate```.
    
  290. 
    
  291. * ``django-admin`` command-line options::
    
  292. 
    
  293.         .. django-admin-option:: --traceback
    
  294. 
    
  295.   To link, use ``:option:`command_name --traceback``` (or omit ``command_name``
    
  296.   for the options shared by all commands like ``--verbosity``).
    
  297. 
    
  298. * Links to Trac tickets (typically reserved for patch release notes)::
    
  299. 
    
  300.         :ticket:`12345`
    
  301. 
    
  302. Django's documentation uses a custom ``console`` directive for documenting
    
  303. command-line examples involving ``django-admin``, ``manage.py``, ``python``,
    
  304. etc.). In the HTML documentation, it renders a two-tab UI, with one tab showing
    
  305. a Unix-style command prompt and a second tab showing a Windows prompt.
    
  306. 
    
  307. For example, you can replace this fragment::
    
  308. 
    
  309.     use this command:
    
  310. 
    
  311.     .. code-block:: console
    
  312. 
    
  313.         $ python manage.py shell
    
  314. 
    
  315. with this one::
    
  316. 
    
  317.     use this command:
    
  318. 
    
  319.     .. console::
    
  320. 
    
  321.         $ python manage.py shell
    
  322. 
    
  323. Notice two things:
    
  324. 
    
  325. * You usually will replace occurrences of the ``.. code-block:: console``
    
  326.   directive.
    
  327. * You don't need to change the actual content of the code example. You still
    
  328.   write it assuming a Unix-y environment (i.e. a ``'$'`` prompt symbol,
    
  329.   ``'/'`` as filesystem path components separator, etc.)
    
  330. 
    
  331. The example above will render a code example block with two tabs. The first
    
  332. one will show:
    
  333. 
    
  334. .. code-block:: console
    
  335. 
    
  336.     $ python manage.py shell
    
  337. 
    
  338. (No changes from what ``.. code-block:: console`` would have rendered).
    
  339. 
    
  340. The second one will show:
    
  341. 
    
  342. .. code-block:: doscon
    
  343. 
    
  344.     ...\> py manage.py shell
    
  345. 
    
  346. .. _documenting-new-features:
    
  347. 
    
  348. Documenting new features
    
  349. ========================
    
  350. 
    
  351. Our policy for new features is:
    
  352. 
    
  353.     All documentation of new features should be written in a way that
    
  354.     clearly designates the features that are only available in the Django
    
  355.     development version. Assume documentation readers are using the latest
    
  356.     release, not the development version.
    
  357. 
    
  358. Our preferred way for marking new features is by prefacing the features'
    
  359. documentation with: "``.. versionadded:: X.Y``", followed by a mandatory
    
  360. blank line and an optional description (indented).
    
  361. 
    
  362. General improvements or other changes to the APIs that should be emphasized
    
  363. should use the "``.. versionchanged:: X.Y``" directive (with the same format
    
  364. as the ``versionadded`` mentioned above.
    
  365. 
    
  366. These ``versionadded`` and ``versionchanged`` blocks should be "self-contained."
    
  367. In other words, since we only keep these annotations around for two releases,
    
  368. it's nice to be able to remove the annotation and its contents without having
    
  369. to reflow, reindent, or edit the surrounding text. For example, instead of
    
  370. putting the entire description of a new or changed feature in a block, do
    
  371. something like this::
    
  372. 
    
  373.     .. class:: Author(first_name, last_name, middle_name=None)
    
  374. 
    
  375.         A person who writes books.
    
  376. 
    
  377.         ``first_name`` is ...
    
  378. 
    
  379.         ...
    
  380. 
    
  381.         ``middle_name`` is ...
    
  382. 
    
  383.         .. versionchanged:: A.B
    
  384. 
    
  385.             The ``middle_name`` argument was added.
    
  386. 
    
  387. Put the changed annotation notes at the bottom of a section, not the top.
    
  388. 
    
  389. Also, avoid referring to a specific version of Django outside a
    
  390. ``versionadded`` or ``versionchanged`` block. Even inside a block, it's often
    
  391. redundant to do so as these annotations render as "New in Django A.B:" and
    
  392. "Changed in Django A.B", respectively.
    
  393. 
    
  394. If a function, attribute, etc. is added, it's also okay to use a
    
  395. ``versionadded`` annotation like this::
    
  396. 
    
  397.     .. attribute:: Author.middle_name
    
  398. 
    
  399.         .. versionadded:: A.B
    
  400. 
    
  401.         An author's middle name.
    
  402. 
    
  403. We can remove the ``.. versionadded:: A.B`` annotation without any indentation
    
  404. changes when the time comes.
    
  405. 
    
  406. Minimizing images
    
  407. =================
    
  408. 
    
  409. Optimize image compression where possible. For PNG files, use OptiPNG and
    
  410. AdvanceCOMP's ``advpng``:
    
  411. 
    
  412. .. code-block:: console
    
  413. 
    
  414.    $ cd docs
    
  415.    $ optipng -o7 -zm1-9 -i0 -strip all `find . -type f -not -path "./_build/*" -name "*.png"`
    
  416.    $ advpng -z4 `find . -type f -not -path "./_build/*" -name "*.png"`
    
  417. 
    
  418. This is based on OptiPNG version 0.7.5. Older versions may complain about the
    
  419. ``-strip all`` option being lossy.
    
  420. 
    
  421. An example
    
  422. ==========
    
  423. 
    
  424. For a quick example of how it all fits together, consider this hypothetical
    
  425. example:
    
  426. 
    
  427. * First, the ``ref/settings.txt`` document could have an overall layout
    
  428.   like this:
    
  429. 
    
  430.   .. code-block:: rst
    
  431. 
    
  432.     ========
    
  433.     Settings
    
  434.     ========
    
  435. 
    
  436.     ...
    
  437. 
    
  438.     .. _available-settings:
    
  439. 
    
  440.     Available settings
    
  441.     ==================
    
  442. 
    
  443.     ...
    
  444. 
    
  445.     .. _deprecated-settings:
    
  446. 
    
  447.     Deprecated settings
    
  448.     ===================
    
  449. 
    
  450.     ...
    
  451. 
    
  452. * Next, the ``topics/settings.txt`` document could contain something like
    
  453.   this:
    
  454. 
    
  455.   .. code-block:: rst
    
  456. 
    
  457.     You can access a :ref:`listing of all available settings
    
  458.     <available-settings>`. For a list of deprecated settings see
    
  459.     :ref:`deprecated-settings`.
    
  460. 
    
  461.     You can find both in the :doc:`settings reference document
    
  462.     </ref/settings>`.
    
  463. 
    
  464.   We use the Sphinx :rst:role:`doc` cross-reference element when we want to
    
  465.   link to another document as a whole and the :rst:role:`ref` element when
    
  466.   we want to link to an arbitrary location in a document.
    
  467. 
    
  468. * Next, notice how the settings are annotated:
    
  469. 
    
  470.   .. code-block:: rst
    
  471. 
    
  472.     .. setting:: ADMINS
    
  473. 
    
  474.     ADMINS
    
  475.     ======
    
  476. 
    
  477.     Default: ``[]`` (Empty list)
    
  478. 
    
  479.     A list of all the people who get code error notifications. When
    
  480.     ``DEBUG=False`` and a view raises an exception, Django will email these people
    
  481.     with the full exception information. Each member of the list should be a tuple
    
  482.     of (Full name, email address). Example::
    
  483. 
    
  484.         [('John', '[email protected]'), ('Mary', '[email protected]')]
    
  485. 
    
  486.     Note that Django will email *all* of these people whenever an error happens.
    
  487.     See :doc:`/howto/error-reporting` for more information.
    
  488. 
    
  489.   This marks up the following header as the "canonical" target for the
    
  490.   setting ``ADMINS``. This means any time I talk about ``ADMINS``,
    
  491.   I can reference it using ``:setting:`ADMINS```.
    
  492. 
    
  493. That's basically how everything fits together.
    
  494. 
    
  495. .. _documentation-spelling-check:
    
  496. 
    
  497. Spelling check
    
  498. ==============
    
  499. 
    
  500. Before you commit your docs, it's a good idea to run the spelling checker.
    
  501. You'll need to install `sphinxcontrib-spelling
    
  502. <https://pypi.org/project/sphinxcontrib-spelling/>`_ first. Then from the
    
  503. ``docs`` directory, run ``make spelling``. Wrong words (if any) along with the
    
  504. file and line number where they occur will be saved to
    
  505. ``_build/spelling/output.txt``.
    
  506. 
    
  507. If you encounter false-positives (error output that actually is correct), do
    
  508. one of the following:
    
  509. 
    
  510. * Surround inline code or brand/technology names with grave accents (`).
    
  511. * Find synonyms that the spell checker recognizes.
    
  512. * If, and only if, you are sure the word you are using is correct - add it
    
  513.   to ``docs/spelling_wordlist`` (please keep the list in alphabetical order).
    
  514. 
    
  515. .. _documentation-link-check:
    
  516. 
    
  517. Link check
    
  518. ==========
    
  519. 
    
  520. Links in documentation can become broken or changed such that they are no
    
  521. longer the canonical link. Sphinx provides a builder that can check whether the
    
  522. links in the documentation are working. From the ``docs`` directory, run ``make
    
  523. linkcheck``. Output is printed to the terminal, but can also be found in
    
  524. ``_build/linkcheck/output.txt`` and ``_build/linkcheck/output.json``.
    
  525. 
    
  526. Entries that have a status of "working" are fine, those that are "unchecked" or
    
  527. "ignored" have been skipped because they either cannot be checked or have
    
  528. matched ignore rules in the configuration.
    
  529. 
    
  530. Entries that have a status of "broken" need to be fixed. Those that have a
    
  531. status of "redirected" may need to be updated to point to the canonical
    
  532. location, e.g. the scheme has changed ``http://`` → ``https://``. In certain
    
  533. cases, we do not want to update a "redirected" link, e.g. a rewrite to always
    
  534. point to the latest or stable version of the documentation, e.g. ``/en/stable/`` →
    
  535. ``/en/3.2/``.
    
  536. 
    
  537. Translating documentation
    
  538. =========================
    
  539. 
    
  540. See :ref:`Localizing the Django documentation <translating-documentation>` if
    
  541. you'd like to help translate the documentation into another language.
    
  542. 
    
  543. .. _django-admin-manpage:
    
  544. 
    
  545. ``django-admin`` man page
    
  546. =========================
    
  547. 
    
  548. Sphinx can generate a manual page for the
    
  549. :doc:`django-admin </ref/django-admin>` command. This is configured in
    
  550. ``docs/conf.py``. Unlike other documentation output, this man page should be
    
  551. included in the Django repository and the releases as
    
  552. ``docs/man/django-admin.1``. There isn't a need to update this file when
    
  553. updating the documentation, as it's updated once as part of the release process.
    
  554. 
    
  555. To generate an updated version of the man page, run ``make man`` in the
    
  556. ``docs`` directory. The new man page will be written in
    
  557. ``docs/_build/man/django-admin.1``.