writing-documentation.txt 18 KB

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