summaryrefslogtreecommitdiff
path: root/docs/internals/contributing/writing-documentation.txt
blob: 9e52e55563a3f339cfc196e4f90747e9a0ad219d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
=====================
Writing documentation
=====================

We place a high importance on consistency and readability of documentation.
After all, Django was created in a journalism environment! So we treat our
documentation like we treat our code: we aim to improve it as often as
possible.

Documentation changes generally come in two forms:

* General improvements: typo corrections, error fixes and better
  explanations through clearer writing and more examples.

* New features: documentation of features that have been added to the
  framework since the last release.

This section explains how writers can craft their documentation changes
in the most useful and least error-prone ways.

Getting the raw documentation
=============================

Though Django's documentation is intended to be read as HTML at
https://docs.djangoproject.com/, we edit it as a collection of text files for
maximum flexibility. These files live in the top-level ``docs/`` directory of a
Django release.

If you'd like to start contributing to our docs, get the development version of
Django from the source code repository
(see :ref:`installing-development-version`). The development version has the
latest-and-greatest documentation, just as it has latest-and-greatest code.
We also backport documentation fixes and improvements, at the discretion of the
committer, to the last release branch. That's because it's highly advantageous
to have the docs for the last release be up-to-date and correct (see
:ref:`differences-between-doc-versions`).

Getting started with Sphinx
===========================

Django's documentation uses the Sphinx__ documentation system, which in turn
is based on docutils__. The basic idea is that lightly-formatted plain-text
documentation is transformed into HTML, PDF, and any other output format.

__ http://sphinx-doc.org/
__ http://docutils.sourceforge.net/

To build the documentation locally, install Sphinx:

.. console::

     $ python -m pip install Sphinx

Then from the ``docs`` directory, build the HTML:

.. console::

     $ make html

To get started contributing, you'll want to read the :ref:`reStructuredText
reference <sphinx:rst-index>`.

Your locally-built documentation will be themed differently than the
documentation at `docs.djangoproject.com <https://docs.djangoproject.com>`_.
This is OK! If your changes look good on your local machine, they'll look good
on the website.

How the documentation is organized
==================================

The documentation is organized into several categories:

* :doc:`Tutorials </intro/index>` take the reader by the hand through a series
  of steps to create something.

  The important thing in a tutorial is to help the reader achieve something
  useful, preferably as early as possible, in order to give them confidence.

  Explain the nature of the problem we're solving, so that the reader
  understands what we're trying to achieve. Don't feel that you need to begin
  with explanations of how things work - what matters is what the reader does,
  not what you explain. It can be helpful to refer back to what you've done and
  explain afterwards.

* :doc:`Topic guides </topics/index>` aim to explain a concept or subject at a
  fairly high level.

  Link to reference material rather than repeat it. Use examples and don't be
  reluctant to explain things that seem very basic to you - it might be the
  explanation someone else needs.

  Providing background context helps a newcomer connect the topic to things
  that they already know.

* :doc:`Reference guides </ref/index>` contain technical reference for APIs.
  They describe the functioning of Django's internal machinery and instruct in
  its use.

  Keep reference material tightly focused on the subject. Assume that the
  reader already understands the basic concepts involved but needs to know or
  be reminded of how Django does it.

  Reference guides aren't the place for general explanation. If you find
  yourself explaining basic concepts, you may want to move that material to a
  topic guide.

* :doc:`How-to guides </howto/index>` are recipes that take the reader through
  steps in key subjects.

  What matters most in a how-to guide is what a user wants to achieve.
  A how-to should always be result-oriented rather than focused on internal
  details of how Django implements whatever is being discussed.

  These guides are more advanced than tutorials and assume some knowledge about
  how Django works. Assume that the reader has followed the tutorials and don't
  hesitate to refer the reader back to the appropriate tutorial rather than
  repeat the same material.

Writing style
=============

When using pronouns in reference to a hypothetical person, such as "a user with
a session cookie", gender neutral pronouns (they/their/them) should be used.
Instead of:

* he or she... use they.
* him or her... use them.
* his or her... use their.
* his or hers... use theirs.
* himself or herself... use themselves.

Try to avoid using words that minimize the difficulty involved in a task or
operation, such as "easily", "simply", "just", "merely", "straightforward", and
so on. People's experience may not match your expectations, and they may become
frustrated when they do not find a step as "straightforward" or "simple" as it
is implied to be.

Commonly used terms
===================

Here are some style guidelines on commonly used terms throughout the
documentation:

* **Django** -- when referring to the framework, capitalize Django. It is
  lowercase only in Python code and in the djangoproject.com logo.

* **email** -- no hyphen.

* **MySQL**, **PostgreSQL**, **SQLite**

* **SQL** -- when referring to SQL, the expected pronunciation should be
  "Ess Queue Ell" and not "sequel". Thus in a phrase like "Returns an
  SQL expression", "SQL" should be preceded by "an" and not "a".

* **Python** -- when referring to the language, capitalize Python.

* **realize**, **customize**, **initialize**, etc. -- use the American
  "ize" suffix, not "ise."

* **subclass** -- it's a single word without a hyphen, both as a verb
  ("subclass that model") and as a noun ("create a subclass").

* **Web**, **World Wide Web**, **the Web** -- note Web is always
  capitalized when referring to the World Wide Web.

* **website** -- use one word, without capitalization.

Django-specific terminology
===========================

* **model** -- it's not capitalized.

* **template** -- it's not capitalized.

* **URLconf** -- use three capitalized letters, with no space before
  "conf."

* **view** -- it's not capitalized.

Guidelines for reStructuredText files
=====================================

These guidelines regulate the format of our reST (reStructuredText)
documentation:

* In section titles, capitalize only initial words and proper nouns.

* Wrap the documentation at 80 characters wide, unless a code example
  is significantly less readable when split over two lines, or for another
  good reason.

* The main thing to keep in mind as you write and edit docs is that the
  more semantic markup you can add the better. So::

      Add ``django.contrib.auth`` to your ``INSTALLED_APPS``...

  Isn't nearly as helpful as::

      Add :mod:`django.contrib.auth` to your :setting:`INSTALLED_APPS`...

  This is because Sphinx will generate proper links for the latter, which
  greatly helps readers.

  You can prefix the target with a ``~`` (that's a tilde) to get just the
  "last bit" of that path. So ``:mod:`~django.contrib.auth``` will just
  display a link with the title "auth".

* Use :mod:`~sphinx.ext.intersphinx` to reference Python's and Sphinx'
  documentation.

* Add ``.. code-block:: <lang>`` to literal blocks so that they get
  highlighted. Prefer relying on automatic highlighting simply using ``::``
  (two colons). This has the benefit that if the code contains some invalid
  syntax, it won't be highlighted. Adding ``.. code-block:: python``, for
  example, will force highlighting despite invalid syntax.

* To improve readability, use ``.. admonition:: Descriptive title`` rather than
  ``.. note::``. Use these boxes sparingly.

* Use these heading styles::

    ===
    One
    ===

    Two
    ===

    Three
    -----

    Four
    ~~~~

    Five
    ^^^^

Django-specific markup
======================

Besides :ref:`Sphinx's built-in markup <sphinx:rst-index>`, Django's docs
define some extra description units:

* Settings::

        .. setting:: INSTALLED_APPS

  To link to a setting, use ``:setting:`INSTALLED_APPS```.

* Template tags::

        .. templatetag:: regroup

  To link, use ``:ttag:`regroup```.

* Template filters::

        .. templatefilter:: linebreaksbr

  To link, use ``:tfilter:`linebreaksbr```.

* Field lookups (i.e. ``Foo.objects.filter(bar__exact=whatever)``)::

        .. fieldlookup:: exact

  To link, use ``:lookup:`exact```.

* ``django-admin`` commands::

        .. django-admin:: migrate

  To link, use ``:djadmin:`migrate```.

* ``django-admin`` command-line options::

        .. django-admin-option:: --traceback

  To link, use ``:option:`command_name --traceback``` (or omit ``command_name``
  for the options shared by all commands like ``--verbosity``).

* Links to Trac tickets (typically reserved for patch release notes)::

        :ticket:`12345`

Django's documentation uses a custom ``console`` directive for documenting
command-line examples involving ``django-admin.py``, ``manage.py``, ``python``,
etc.). In the HTML documentation, it renders a two-tab UI, with one tab showing
a Unix-style command prompt and a second tab showing a Windows prompt.

For example, you can replace this fragment::

    use this command:

    .. code-block:: console

        $ python manage.py shell

with this one::

    use this command:

    .. console::

        $ python manage.py shell

Notice two things:

* You usually will replace occurrences of the ``.. code-block:: console``
  directive.
* You don't need to change the actual content of the code example. You still
  write it assuming a Unix-y environment (i.e. a ``'$'`` prompt symbol,
  ``'/'`` as filesystem path components separator, etc.)

The example above will render a code example block with two tabs. The first
one will show:

.. code-block:: console

    $ python manage.py shell

(No changes from what ``.. code-block:: console`` would have rendered).

The second one will show:

.. code-block:: doscon

    ...\> py manage.py shell

.. _documenting-new-features:

Documenting new features
========================

Our policy for new features is:

    All documentation of new features should be written in a way that
    clearly designates the features are only available in the Django
    development version. Assume documentation readers are using the latest
    release, not the development version.

Our preferred way for marking new features is by prefacing the features'
documentation with: "``.. versionadded:: X.Y``", followed by a mandatory
blank line and an optional description (indented).

General improvements, or other changes to the APIs that should be emphasized
should use the "``.. versionchanged:: X.Y``" directive (with the same format
as the ``versionadded`` mentioned above.

These ``versionadded`` and ``versionchanged`` blocks should be "self-contained."
In other words, since we only keep these annotations around for two releases,
it's nice to be able to remove the annotation and its contents without having
to reflow, reindent, or edit the surrounding text. For example, instead of
putting the entire description of a new or changed feature in a block, do
something like this::

    .. class:: Author(first_name, last_name, middle_name=None)

        A person who writes books.

        ``first_name`` is ...

        ...

        ``middle_name`` is ...

        .. versionchanged:: A.B

            The ``middle_name`` argument was added.

Put the changed annotation notes at the bottom of a section, not the top.

Also, avoid referring to a specific version of Django outside a
``versionadded`` or ``versionchanged`` block. Even inside a block, it's often
redundant to do so as these annotations render as "New in Django A.B:" and
"Changed in Django A.B", respectively.

If a function, attribute, etc. is added, it's also okay to use a
``versionadded`` annotation like this::

    .. attribute:: Author.middle_name

        .. versionadded:: A.B

        An author's middle name.

We can simply remove the ``.. versionadded:: A.B`` annotation without any
indentation changes when the time comes.

Minimizing images
=================

Optimize image compression where possible. For PNG files, use OptiPNG and
AdvanceCOMP's ``advpng``:

.. code-block:: console

   $ cd docs
   $ optipng -o7 -zm1-9 -i0 -strip all `find . -type f -not -path "./_build/*" -name "*.png"`
   $ advpng -z4 `find . -type f -not -path "./_build/*" -name "*.png"`

This is based on OptiPNG version 0.7.5. Older versions may complain about the
``--strip all`` option being lossy.

An example
==========

For a quick example of how it all fits together, consider this hypothetical
example:

* First, the ``ref/settings.txt`` document could have an overall layout
  like this:

  .. code-block:: rst

    ========
    Settings
    ========

    ...

    .. _available-settings:

    Available settings
    ==================

    ...

    .. _deprecated-settings:

    Deprecated settings
    ===================

    ...

* Next, the ``topics/settings.txt`` document could contain something like
  this:

  .. code-block:: rst

    You can access a :ref:`listing of all available settings
    <available-settings>`. For a list of deprecated settings see
    :ref:`deprecated-settings`.

    You can find both in the :doc:`settings reference document
    </ref/settings>`.

  We use the Sphinx :rst:role:`doc` cross reference element when we want to
  link to another document as a whole and the :rst:role:`ref` element when
  we want to link to an arbitrary location in a document.

* Next, notice how the settings are annotated:

  .. code-block:: rst

    .. setting:: ADMINS

    ADMINS
    ======

    Default: ``[]`` (Empty list)

    A list of all the people who get code error notifications. When
    ``DEBUG=False`` and a view raises an exception, Django will email these people
    with the full exception information. Each member of the list should be a tuple
    of (Full name, email address). Example::

        [('John', 'john@example.com'), ('Mary', 'mary@example.com')]

    Note that Django will email *all* of these people whenever an error happens.
    See :doc:`/howto/error-reporting` for more information.

  This marks up the following header as the "canonical" target for the
  setting ``ADMINS``. This means any time I talk about ``ADMINS``,
  I can reference it using ``:setting:`ADMINS```.

That's basically how everything fits together.

.. _documentation-spelling-check:

Spelling check
==============

Before you commit your docs, it's a good idea to run the spelling checker.
You'll need to install a couple packages first:

* `pyenchant <https://pypi.org/project/pyenchant/>`_ (which requires
  `enchant <https://www.abisource.com/projects/enchant/>`_)

* `sphinxcontrib-spelling
  <https://pypi.org/project/sphinxcontrib-spelling/>`_

Then from the ``docs`` directory, run ``make spelling``. Wrong words (if any)
along with the file and line number where they occur will be saved to
``_build/spelling/output.txt``.

If you encounter false-positives (error output that actually is correct), do
one of the following:

* Surround inline code or brand/technology names with grave accents (`).
* Find synonyms that the spell checker recognizes.
* If, and only if, you are sure the word you are using is correct - add it
  to ``docs/spelling_wordlist`` (please keep the list in alphabetical order).

Translating documentation
=========================

See :ref:`Localizing the Django documentation <translating-documentation>` if
you'd like to help translate the documentation into another language.

.. _django-admin-manpage:

``django-admin`` man page
=========================

Sphinx can generate a manual page for the
:doc:`django-admin </ref/django-admin>` command. This is configured in
``docs/conf.py``. Unlike other documentation output, this man page should be
included in the Django repository and the releases as
``docs/man/django-admin.1``. There isn't a need to update this file when
updating the documentation, as it's updated once as part of the release process.

To generate an updated version of the man page, run ``make man`` in the
``docs`` directory. The new man page will be written in
``docs/_build/man/django-admin.1``.