writing-documentation.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 -- ``sudo 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. Commonly used terms
  51. -------------------
  52. Here are some style guidelines on commonly used terms throughout the
  53. documentation:
  54. * **Django** -- when referring to the framework, capitalize Django. It is
  55. lowercase only in Python code and in the djangoproject.com logo.
  56. * **email** -- no hyphen.
  57. * **MySQL**, **PostgreSQL**, **SQLite**
  58. * **SQL** -- when referring to SQL, the expected pronunciation should be
  59. "Ess Queue Ell" and not "sequel". Thus in a phrase like "Returns an
  60. SQL expression", "SQL" should be preceded by "an" and not "a".
  61. * **Python** -- when referring to the language, capitalize Python.
  62. * **realize**, **customize**, **initialize**, etc. -- use the American
  63. "ize" suffix, not "ise."
  64. * **subclass** -- it's a single word without a hyphen, both as a verb
  65. ("subclass that model") and as a noun ("create a subclass").
  66. * **Web**, **World Wide Web**, **the Web** -- note Web is always
  67. capitalized when referring to the World Wide Web.
  68. * **Web site** -- use two words, with Web capitalized.
  69. Django-specific terminology
  70. ---------------------------
  71. * **model** -- it's not capitalized.
  72. * **template** -- it's not capitalized.
  73. * **URLconf** -- use three capitalized letters, with no space before
  74. "conf."
  75. * **view** -- it's not capitalized.
  76. Guidelines for reStructuredText files
  77. -------------------------------------
  78. These guidelines regulate the format of our reST (reStructuredText)
  79. documentation:
  80. * In section titles, capitalize only initial words and proper nouns.
  81. * Wrap the documentation at 80 characters wide, unless a code example
  82. is significantly less readable when split over two lines, or for another
  83. good reason.
  84. * The main thing to keep in mind as you write and edit docs is that the
  85. more semantic markup you can add the better. So::
  86. Add ``django.contrib.auth`` to your ``INSTALLED_APPS``...
  87. Isn't nearly as helpful as::
  88. Add :mod:`django.contrib.auth` to your :setting:`INSTALLED_APPS`...
  89. This is because Sphinx will generate proper links for the latter, which
  90. greatly helps readers. There's basically no limit to the amount of
  91. useful markup you can add.
  92. * Use :mod:`~sphinx.ext.intersphinx` to reference Python's and Sphinx'
  93. documentation.
  94. Django-specific markup
  95. ----------------------
  96. Besides the `Sphinx built-in markup`__, Django's docs defines some extra
  97. description units:
  98. __ http://sphinx-doc.org/markup/desc.html
  99. * Settings::
  100. .. setting:: INSTALLED_APPS
  101. To link to a setting, use ``:setting:`INSTALLED_APPS```.
  102. * Template tags::
  103. .. templatetag:: regroup
  104. To link, use ``:ttag:`regroup```.
  105. * Template filters::
  106. .. templatefilter:: linebreaksbr
  107. To link, use ``:tfilter:`linebreaksbr```.
  108. * Field lookups (i.e. ``Foo.objects.filter(bar__exact=whatever)``)::
  109. .. fieldlookup:: exact
  110. To link, use ``:lookup:`exact```.
  111. * ``django-admin`` commands::
  112. .. django-admin:: migrate
  113. To link, use ``:djadmin:`migrate```.
  114. * ``django-admin`` command-line options::
  115. .. django-admin-option:: --traceback
  116. To link, use ``:djadminopt:`--traceback```.
  117. .. _documenting-new-features:
  118. Documenting new features
  119. ------------------------
  120. Our policy for new features is:
  121. All documentation of new features should be written in a way that
  122. clearly designates the features are only available in the Django
  123. development version. Assume documentation readers are using the latest
  124. release, not the development version.
  125. Our preferred way for marking new features is by prefacing the features'
  126. documentation with: "``.. versionadded:: X.Y``", followed by a a mandatory
  127. blank line and an optional content (indented).
  128. General improvements, or other changes to the APIs that should be emphasized
  129. should use the "``.. versionchanged:: X.Y``" directive (with the same format
  130. as the ``versionadded`` mentioned above.
  131. An example
  132. ----------
  133. For a quick example of how it all fits together, consider this hypothetical
  134. example:
  135. * First, the ``ref/settings.txt`` document could have an overall layout
  136. like this:
  137. .. code-block:: rst
  138. ========
  139. Settings
  140. ========
  141. ...
  142. .. _available-settings:
  143. Available settings
  144. ==================
  145. ...
  146. .. _deprecated-settings:
  147. Deprecated settings
  148. ===================
  149. ...
  150. * Next, the ``topics/settings.txt`` document could contain something like
  151. this:
  152. .. code-block:: rst
  153. You can access a :ref:`listing of all available settings
  154. <available-settings>`. For a list of deprecated settings see
  155. :ref:`deprecated-settings`.
  156. You can find both in the :doc:`settings reference document
  157. </ref/settings>`.
  158. We use the Sphinx :rst:role:`doc` cross reference element when we want to
  159. link to another document as a whole and the :rst:role:`ref` element when
  160. we want to link to an arbitrary location in a document.
  161. * Next, notice how the settings are annotated:
  162. .. code-block:: rst
  163. .. setting:: ADMIN_FOR
  164. ADMIN_FOR
  165. ---------
  166. Default: ``()`` (Empty tuple)
  167. Used for admin-site settings modules, this should be a tuple of
  168. settings modules (in the format ``'foo.bar.baz'``) for which this site
  169. is an admin.
  170. The admin site uses this in its automatically-introspected
  171. documentation of models, views and template tags.
  172. This marks up the following header as the "canonical" target for the
  173. setting ``ADMIN_FOR`` This means any time I talk about ``ADMIN_FOR``,
  174. I can reference it using ``:setting:`ADMIN_FOR```.
  175. That's basically how everything fits together.
  176. .. _improving-the-documentation:
  177. Improving the documentation
  178. ---------------------------
  179. A few small improvements can be made to make the documentation read and
  180. look better:
  181. * Most of the various ``index.txt`` documents have *very* short or even
  182. non-existent intro text. Each of those documents needs a good short
  183. intro the content below that point.
  184. * The glossary is very perfunctory. It needs to be filled out.
  185. * Add more metadata targets. Lots of places look like::
  186. ``File.close()``
  187. ~~~~~~~~~~~~~~~~
  188. \... these should be::
  189. .. method:: File.close()
  190. That is, use metadata instead of titles.
  191. * Add more links -- nearly everything that's an inline code literal
  192. right now can probably be turned into a xref.
  193. See the ``literals_to_xrefs.py`` file in ``_ext`` -- it's a shell script
  194. to help do this work.
  195. This will probably be a continuing, never-ending project.
  196. * Add `info field lists`__ where appropriate.
  197. __ http://sphinx-doc.org/markup/desc.html#info-field-lists
  198. * Whenever possible, use links. So, use ``:setting:`ADMIN_FOR``` instead
  199. of ````ADMIN_FOR````.
  200. * Use directives where appropriate. Some directives
  201. (e.g. ``.. setting::``) are prefix-style directives; they go *before*
  202. the unit they're describing. These are known as "crossref" directives.
  203. Others (e.g. ``.. class::``) generate their own markup; these should go
  204. inside the section they're describing. These are called
  205. "description units".
  206. You can tell which are which by looking at in
  207. :file:`_ext/djangodocs.py`; it registers roles as one of the other.
  208. * Add ``.. code-block:: <lang>`` to literal blocks so that they get
  209. highlighted.
  210. * When referring to classes/functions/modules, etc., you'll want to use
  211. the fully-qualified name of the target
  212. (``:class:`django.contrib.contenttypes.models.ContentType```).
  213. Since this doesn't look all that awesome in the output -- it shows the
  214. entire path to the object -- you can prefix the target with a ``~``
  215. (that's a tilde) to get just the "last bit" of that path. So
  216. ``:class:`~django.contrib.contenttypes.models.ContentType``` will just
  217. display a link with the title "ContentType".