writing-documentation.txt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. =====================
  2. Writing documentation
  3. =====================
  4. We place a high importance on 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 ``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 latest-and-greatest code.
  25. We also backport documentation fixes and improvements, at the discretion of the
  26. committer, to the last release branch. That's because it's highly advantageous
  27. to 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. __ http://sphinx-doc.org/
  35. __ http://docutils.sourceforge.net/
  36. To actually build the documentation locally, you'll currently need to install
  37. Sphinx -- ``pip install Sphinx`` should do the trick.
  38. .. note::
  39. Building the Django documentation requires Sphinx 1.0.2 or newer. Sphinx
  40. also requires the Pygments__ library for syntax highlighting; building the
  41. Django documentation requires Pygments 1.1 or newer (a new-enough version
  42. should automatically be installed along with Sphinx).
  43. __ http://pygments.org
  44. Then, building the HTML is easy; just ``make html`` (or ``make.bat html`` on
  45. Windows) from the ``docs`` directory.
  46. To get started contributing, you'll want to read the :ref:`reStructuredText
  47. Primer <sphinx:rst-primer>`. After that, you'll want to read about the
  48. :ref:`Sphinx-specific markup <sphinx:sphinxmarkup>` that's used to manage
  49. metadata, indexing, and cross-references.
  50. How the documentation is organized
  51. ----------------------------------
  52. The documentation is organized into several categories:
  53. * :doc:`Tutorials </intro/index>` take the reader by the hand through a series
  54. of steps to create something.
  55. The important thing in a tutorial is to help the reader achieve something
  56. useful, preferably as early as possible, in order to give them confidence.
  57. Explain the nature of the problem we're solving, so that the reader
  58. understands what we're trying to achieve. Don't feel that you need to begin
  59. with explanations of how things work - what matters is what the reader does,
  60. not what you explain. It can be helpful to refer back to what you've done and
  61. explain afterwards.
  62. * :doc:`Topic guides </topics/index>` aim to explain a concept or subject at a
  63. fairly high level.
  64. Link to reference material rather than repeat it. Use examples and don't be
  65. reluctant to explain things that seem very basic to you - it might be the
  66. explanation someone else needs.
  67. Providing background context helps a newcomer connect the topic to things
  68. that they already know.
  69. * :doc:`Reference guides </ref/index>` contain technical reference for APIs.
  70. They describe the functioning of Django's internal machinery and instruct in
  71. its use.
  72. Keep reference material tightly focused on the subject. Assume that the
  73. reader already understands the basic concepts involved but needs to know or
  74. be reminded of how Django does it.
  75. Reference guides aren't the place for general explanation. If you find
  76. yourself explaining basic concepts, you may want to move that material to a
  77. topic guide.
  78. * :doc:`How-to guides </howto/index>` are recipes that take the reader through
  79. steps in key subjects.
  80. What matters most in a how-to guide is what a user wants to achieve.
  81. A how-to should always be result-oriented rather than focused on internal
  82. details of how Django implements whatever is being discussed.
  83. These guides are more advanced than tutorials and assume some knowledge about
  84. how Django works. Assume that the reader has followed the tutorials and don't
  85. hesitate to refer the reader back to the appropriate tutorial rather than
  86. repeat the same material.
  87. Writing style
  88. -------------
  89. When using pronouns in reference to a hypothetical person, such as "a user with
  90. a session cookie", gender neutral pronouns (they/their/them) should be used.
  91. Instead of:
  92. * he or she... use they.
  93. * him or her... use them.
  94. * his or her... use their.
  95. * his or hers... use theirs.
  96. * himself or herself... use themselves.
  97. Commonly used terms
  98. -------------------
  99. Here are some style guidelines on commonly used terms throughout the
  100. documentation:
  101. * **Django** -- when referring to the framework, capitalize Django. It is
  102. lowercase only in Python code and in the djangoproject.com logo.
  103. * **email** -- no hyphen.
  104. * **MySQL**, **PostgreSQL**, **SQLite**
  105. * **SQL** -- when referring to SQL, the expected pronunciation should be
  106. "Ess Queue Ell" and not "sequel". Thus in a phrase like "Returns an
  107. SQL expression", "SQL" should be preceded by "an" and not "a".
  108. * **Python** -- when referring to the language, capitalize Python.
  109. * **realize**, **customize**, **initialize**, etc. -- use the American
  110. "ize" suffix, not "ise."
  111. * **subclass** -- it's a single word without a hyphen, both as a verb
  112. ("subclass that model") and as a noun ("create a subclass").
  113. * **Web**, **World Wide Web**, **the Web** -- note Web is always
  114. capitalized when referring to the World Wide Web.
  115. * **website** -- use one word, without capitalization.
  116. Django-specific terminology
  117. ---------------------------
  118. * **model** -- it's not capitalized.
  119. * **template** -- it's not capitalized.
  120. * **URLconf** -- use three capitalized letters, with no space before
  121. "conf."
  122. * **view** -- it's not capitalized.
  123. Guidelines for reStructuredText files
  124. -------------------------------------
  125. These guidelines regulate the format of our reST (reStructuredText)
  126. documentation:
  127. * In section titles, capitalize only initial words and proper nouns.
  128. * Wrap the documentation at 80 characters wide, unless a code example
  129. is significantly less readable when split over two lines, or for another
  130. good reason.
  131. * The main thing to keep in mind as you write and edit docs is that the
  132. more semantic markup you can add the better. So::
  133. Add ``django.contrib.auth`` to your ``INSTALLED_APPS``...
  134. Isn't nearly as helpful as::
  135. Add :mod:`django.contrib.auth` to your :setting:`INSTALLED_APPS`...
  136. This is because Sphinx will generate proper links for the latter, which
  137. greatly helps readers. There's basically no limit to the amount of
  138. useful markup you can add.
  139. * Use :mod:`~sphinx.ext.intersphinx` to reference Python's and Sphinx'
  140. documentation.
  141. Django-specific markup
  142. ----------------------
  143. Besides the `Sphinx built-in markup`__, Django's docs defines some extra
  144. description units:
  145. __ http://sphinx-doc.org/markup/
  146. * Settings::
  147. .. setting:: INSTALLED_APPS
  148. To link to a setting, use ``:setting:`INSTALLED_APPS```.
  149. * Template tags::
  150. .. templatetag:: regroup
  151. To link, use ``:ttag:`regroup```.
  152. * Template filters::
  153. .. templatefilter:: linebreaksbr
  154. To link, use ``:tfilter:`linebreaksbr```.
  155. * Field lookups (i.e. ``Foo.objects.filter(bar__exact=whatever)``)::
  156. .. fieldlookup:: exact
  157. To link, use ``:lookup:`exact```.
  158. * ``django-admin`` commands::
  159. .. django-admin:: migrate
  160. To link, use ``:djadmin:`migrate```.
  161. * ``django-admin`` command-line options::
  162. .. django-admin-option:: --traceback
  163. To link, use ``:djadminopt:`--traceback```.
  164. * Links to Trac tickets (typically reserved for patch release notes)::
  165. :ticket:`12345`
  166. .. _documenting-new-features:
  167. Documenting new features
  168. ------------------------
  169. Our policy for new features is:
  170. All documentation of new features should be written in a way that
  171. clearly designates the features are only available in the Django
  172. development version. Assume documentation readers are using the latest
  173. release, not the development version.
  174. Our preferred way for marking new features is by prefacing the features'
  175. documentation with: "``.. versionadded:: X.Y``", followed by a mandatory
  176. blank line and an optional description (indented).
  177. General improvements, or other changes to the APIs that should be emphasized
  178. should use the "``.. versionchanged:: X.Y``" directive (with the same format
  179. as the ``versionadded`` mentioned above.
  180. These ``versionadded`` and ``versionchanged`` blocks should be "self-contained."
  181. In other words, since we only keep these annotations around for two releases,
  182. it's nice to be able to remove the annotation and its contents without having
  183. to reflow, reindent, or edit the surrounding text. For example, instead of
  184. putting the entire description of a new or changed feature in a block, do
  185. something like this::
  186. .. class:: Author(first_name, last_name, middle_name=None)
  187. A person who writes books.
  188. ``first_name`` is ...
  189. ...
  190. ``middle_name`` is ...
  191. .. versionchanged:: A.B
  192. The ``middle_name`` argument was added.
  193. Put the changed annotation notes at the bottom of a section, not the top.
  194. Also, avoid referring to a specific version of Django outside a
  195. ``versionadded`` or ``versionchanged`` block. Even inside a block, it's often
  196. redundant to do so as these annotations render as "New in Django A.B:" and
  197. "Changed in Django A.B", respectively.
  198. If a function, attribute, etc. is added, it's also okay to use a
  199. ``versionadded`` annotation like this::
  200. .. attribute:: Author.middle_name
  201. .. versionadded:: A.B
  202. An author's middle name.
  203. We can simply remove the ``.. versionadded:: A.B`` annotation without any
  204. indentation changes when the time comes.
  205. Minimizing images
  206. -----------------
  207. Optimize image compression where possible. For PNG files, use OptiPNG and
  208. AdvanceCOMP's ``advpng``:
  209. .. code-block:: console
  210. $ cd docs/
  211. $ optipng -o7 -zm1-9 -i0 -strip all `find . -type f -not -path "./_build/*" -name "*.png"`
  212. $ advpng -z4 `find . -type f -not -path "./_build/*" -name "*.png"`
  213. This is based on OptiPNG version 0.7.5. Older versions may complain about the
  214. ``--strip all`` option being lossy.
  215. An example
  216. ----------
  217. For a quick example of how it all fits together, consider this hypothetical
  218. example:
  219. * First, the ``ref/settings.txt`` document could have an overall layout
  220. like this:
  221. .. code-block:: rst
  222. ========
  223. Settings
  224. ========
  225. ...
  226. .. _available-settings:
  227. Available settings
  228. ==================
  229. ...
  230. .. _deprecated-settings:
  231. Deprecated settings
  232. ===================
  233. ...
  234. * Next, the ``topics/settings.txt`` document could contain something like
  235. this:
  236. .. code-block:: rst
  237. You can access a :ref:`listing of all available settings
  238. <available-settings>`. For a list of deprecated settings see
  239. :ref:`deprecated-settings`.
  240. You can find both in the :doc:`settings reference document
  241. </ref/settings>`.
  242. We use the Sphinx :rst:role:`doc` cross reference element when we want to
  243. link to another document as a whole and the :rst:role:`ref` element when
  244. we want to link to an arbitrary location in a document.
  245. * Next, notice how the settings are annotated:
  246. .. code-block:: rst
  247. .. setting:: ADMINS
  248. ADMINS
  249. ------
  250. Default: ``[]`` (Empty list)
  251. A list of all the people who get code error notifications. When
  252. ``DEBUG=False`` and a view raises an exception, Django will email these people
  253. with the full exception information. Each member of the list should be a tuple
  254. of (Full name, email address). Example::
  255. [('John', 'john@example.com'), ('Mary', 'mary@example.com')]
  256. Note that Django will email *all* of these people whenever an error happens.
  257. See :doc:`/howto/error-reporting` for more information.
  258. This marks up the following header as the "canonical" target for the
  259. setting ``ADMINS``. This means any time I talk about ``ADMINS``,
  260. I can reference it using ``:setting:`ADMINS```.
  261. That's basically how everything fits together.
  262. .. _improving-the-documentation:
  263. Improving the documentation
  264. ---------------------------
  265. A few small improvements can be made to make the documentation read and
  266. look better:
  267. * Most of the various ``index.txt`` documents have *very* short or even
  268. non-existent intro text. Each of those documents needs a good short
  269. intro the content below that point.
  270. * The glossary is very perfunctory. It needs to be filled out.
  271. * Add more metadata targets. Lots of places look like::
  272. ``File.close()``
  273. ~~~~~~~~~~~~~~~~
  274. \... these should be::
  275. .. method:: File.close()
  276. That is, use metadata instead of titles.
  277. * Whenever possible, use links. So, use ``:setting:`ADMINS``` instead
  278. of ````ADMINS````.
  279. * Use directives where appropriate. Some directives
  280. (e.g. ``.. setting::``) are prefix-style directives; they go *before*
  281. the unit they're describing. These are known as "crossref" directives.
  282. Others (e.g. ``.. class::``) generate their own markup; these should go
  283. inside the section they're describing. These are called
  284. "description units".
  285. You can tell which are which by looking at in
  286. :file:`_ext/djangodocs.py`; it registers roles as one of the other.
  287. * Add ``.. code-block:: <lang>`` to literal blocks so that they get
  288. highlighted.
  289. * When referring to classes/functions/modules, etc., you'll want to use
  290. the fully-qualified name of the target
  291. (``:class:`django.contrib.contenttypes.models.ContentType```).
  292. Since this doesn't look all that awesome in the output -- it shows the
  293. entire path to the object -- you can prefix the target with a ``~``
  294. (that's a tilde) to get just the "last bit" of that path. So
  295. ``:class:`~django.contrib.contenttypes.models.ContentType``` will just
  296. display a link with the title "ContentType".
  297. .. _documentation-spelling-check:
  298. Spelling check
  299. --------------
  300. Before you commit your docs, it's a good idea to run the spelling checker.
  301. You'll need to install a couple packages first:
  302. * `pyenchant <https://pypi.python.org/pypi/pyenchant/>`_ (which requires
  303. `enchant <http://www.abisource.com/projects/enchant/>`_)
  304. * `sphinxcontrib-spelling
  305. <https://pypi.python.org/pypi/sphinxcontrib-spelling/>`_
  306. Then from the ``docs`` directory, run ``make spelling``. Wrong words (if any)
  307. along with the file and line number where they occur will be saved to
  308. ``_build/spelling/output.txt``.
  309. If you encounter false-positives (error output that actually is correct), do
  310. one of the following:
  311. * Surround inline code or brand/technology names with grave accents (`).
  312. * Find synonyms that the spell checker recognizes.
  313. * If, and only if, you are sure the word you are using is correct - add it
  314. to ``docs/spelling_wordlist`` (please keep the list in alphabetical order).
  315. Translating documentation
  316. -------------------------
  317. See :ref:`Localizing the Django documentation <translating-documentation>` if
  318. you'd like to help translate the documentation into another language.
  319. .. _django-admin-manpage:
  320. ``django-admin`` man page
  321. -------------------------
  322. Sphinx can generate a manual page for the
  323. :doc:`django-admin </ref/django-admin>` command. This is configured in
  324. ``docs/conf.py``. Unlike other documentation output, this man page should be
  325. included in the Django repository and the releases as
  326. ``docs/man/django-admin.1``. There isn't a need to update this file when
  327. updating the documentation, as it's updated once as part of the release process.
  328. To generate an updated version of the man page, run ``make man`` in the
  329. ``docs`` directory. The new man page will be written in
  330. ``docs/_build/man/django-admin.1``.