writing-documentation.txt 20 KB

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