编写文档

    一般来说,文档会在以下两种情况时更新:

    • 一般改进:通过更清晰的书写和更多示例,更正、修复文档错误,更好的解释功能。
    • 新特性:自上一个版本发布后,添加到框架中的功能文档。

    本节介绍文档作者如何以最有用和最不容易出错的方式修改文档。

    Django 文档可在 以网页的形式阅读,但我们以一种更灵活的方式编辑它——一系列的文本文件。这些文件位于 Django 的每个发布分支的顶级目录 docs/ 下。

    如果你想修改文档,请先从源码仓库获取开发版的 Django (参见 安装开发版)。开发版拥有最新最好的文档,就像它拥有最新最好的代码一样。我们也会在最新发布分支上提交针对文档的修复和优化(取决于提交者)。这是因为让最新版本的文档保持最新和正确是非常有利的(参见 不同版本文档间的区别)。

    开始使用 Sphinx

    Django 的文档使用 Sphinx 文档系统——基于 。基本思想是将轻量格式话的纯文本转化为 HTML,PDF 或其它任意输出格式。

    To build the documentation locally, install Sphinx:

    Linux/MacOS    Windows

    1. ...\> py -m pip install Sphinx

    Then from the docs directory, build the HTML:

    Linux/MacOS    Windows

    1. $ make html
    1. ...\> make.bat html

    编写文档前,你需要阅读 reStructuredText 指引

    本地构建的文档的主题会和 上的不同。没事!如果你修改后的文档在本地看起来没啥问题,那么在网站上也会没问题。

    文档是如何组成

    文档被分为以下几个类别:

    • 教程 通过几步手把手的教学帮助读者创建一个小玩意。

      教程的目的是帮助读者尽可能早地实现一些有用的东西,以便给他们带来信心。

      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.

    • 旨在在一个较高的层次介绍一个原则或主题。

      链接至参考资料而不要重复它。使用示例时,不要不情愿解释对您而言非常基本的事物——它对别人而言可能需要解释。

      提供背景信息有助于新人将主题和他们已知的东西联系起来。

    • Reference guides 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.

    • 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.

    书写格式

    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:

    • 他或她……使用他们。
    • him 或 her… 使用 them。
    • 他的或她的……使用他们的。
    • his 或 hers… 使用 theirs。
    • himself 或 herself… 使用 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.

    以下是整个文档中常用术语的一些格式指南:

    • Django — 当提及该框架时,大写Django。它仅在Python代码中和djangoproject.com徽标中使用小写字母。
    • email — 无连字符。
    • MySQL, PostgreSQL, SQLite
    • SQL — 当提及SQL时,预期的发音应该是“Ess Queue Ell”而不是“sequel”。因此,在诸如“Returns an SQL expression”之类的短语中,“SQL”前应该使用“an”而不是“a”。
    • Python — 当提及该语言时大写。
    • realize, customize, initialize, etc. — 使用美式的“ize”后缀,而不是“ise”。
    • subclass — 它是一个没有连字符的单个单词,既作为动词(“子类模型”)又作为名词(“创建子类”)。
    • Web, World Wide Web, the Web — 指万维网时注意Web总是大写。
    • website — 用一个单词表示,不大写。

    Django专用术语

    • model (模型) — 它不是大写的。
    • template — 它不是大写的。
    • URLconf — 使用了三个大写字母,在“conf”之前没有空格。
    • view — 它不是大写的。

    reStructuredText 文件语法指南

    这些准则规定了我们的reST(reStructuredText)文档格式:

    • 将文档以80个字符宽包装,除非将代码示例分成两行或出于其他充分理由而使可读性大大降低。

    • 在编写和编辑文档时要记住的主要事情是,可以添加的语义标记越多越好。 所以:

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

      远没有:

      1. 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 only the “last bit” of that path. So :mod:`~django.contrib.auth` will display a link with the title “auth”.

    • 使用 intersphinx 来引用Python和Sphinx的文档。

    • Add .. code-block:: <lang> to literal blocks so that they get highlighted. Prefer relying on automatic highlighting 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:

      1. ===
      2. One
      3. ===
      4. Two
      5. ===
      6. Three
      7. -----
      8. Four
      9. ~~~~
      10. Five
      11. ^^^^
    • Use to reference RFC and try to link to the relevant section if possible. For example, use :rfc:`2324#section-2.3.2` or :rfc:`Custom link text <2324#section-2.3.2>` .

    • Use :pep: to reference a Python Enhancement Proposal (PEP) and try to link to the relevant section if possible. For example, use :pep:`20#easter-egg` or :pep:`Easter Egg <20#easter-egg>` .

    • Use to refer to a MIME Type unless the value is quoted for a code example.

    • Use :envvar: to refer to an environment variable. You may also need to define a reference to the documentation for that environment variable using .

    Django特定的标记

    Besides Sphinx’s built-in markup, Django’s docs define some extra description units:

    • 配置:

      1. .. setting:: INSTALLED_APPS

      为了连接配置,请使用配置 :setting:`INSTALLED_APPS`

    • 模板标签:

      为了链接,请使用 :ttag:`regroup`

    • 模板过滤器:

      1. .. templatefilter:: linebreaksbr

      为了链接,请使用 :tfilter:`linebreaksbr`

    • 字段查询(例如 Foo.objects.filter(bar__exact=whatever)):

      1. .. fieldlookup:: exact

      为了链接,请使用 :lookup:`exact`

    • django-admin 管理员命令:

      1. .. django-admin:: migrate

      To link, use :djadmin:`migrate` .

    • django-admin 管理员命令行选项:

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

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

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

      1. :ticket:`12345`

    Django’s documentation uses a custom console directive for documenting command-line examples involving django-admin, 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.

    1. use this command:
    2. .. code-block:: console
    3. $ python manage.py shell

    with this one:

    1. use this command:
    2. .. console::
    3. $ 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:

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

    The second one will show:

    1. ...\> py manage.py shell

    我们对新功能的政策是:

    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).

    常规改进或应强调的其他API更改应使用 “.. versionchanged:: X.Y“ 指令(与上述 versionadded 相同。

    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:

    1. .. class:: Author(first_name, last_name, middle_name=None)
    2. A person who writes books.
    3. ``first_name`` is ...
    4. ...
    5. ``middle_name`` is ...
    6. .. versionchanged:: A.B

    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:

    1. .. attribute:: Author.middle_name
    2. .. versionadded:: A.B
    3. An author's middle name.

    We can 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:

    1. $ cd docs
    2. $ optipng -o7 -zm1-9 -i0 -strip all `find . -type f -not -path "./_build/*" -name "*.png"`
    3. $ 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.

    一个例子

    有关如何将它们组合在一起的快速示例,请考虑以下假设示例:

    • 首先, ref/settings.txt 配置文件可能具有如下总体布局:

      1. ========
      2. ========
      3. ...
      4. .. _available-settings:
      5. Available settings
      6. ==================
      7. ...
      8. .. _deprecated-settings:
      9. Deprecated settings
      10. ===================
      11. ...
    • 接下来, topics/settings.txt 配置文档可能包含以下内容:

      1. You can access a :ref:`listing of all available settings
      2. <available-settings>`. For a list of deprecated settings see
      3. :ref:`deprecated-settings`.
      4. You can find both in the :doc:`settings reference document
      5. </ref/settings>`.

      当我们想链接到另一个文档时,我们使用 交叉引用元素;当我们想链接到文档中的任意的位置时,请使用 ref 元素。

    • 接下来,请注意配置的注释方式:

      1. .. setting:: ADMINS
      2. ADMINS
      3. ======
      4. Default: ``[]`` (Empty list)
      5. A list of all the people who get code error notifications. When
      6. ``DEBUG=False`` and a view raises an exception, Django will email these people
      7. with the full exception information. Each member of the list should be a tuple
      8. of (Full name, email address). Example::
      9. [('John', 'john@example.com'), ('Mary', 'mary@example.com')]
      10. Note that Django will email *all* of these people whenever an error happens.
      11. 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` .

    基本上,这就是所有东西融合在一起的方式。

    拼写检查

    Before you commit your docs, it’s a good idea to run the spelling checker. You’ll need to install first. 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 (`).
    • 如果,只是如果,你确定你的单词拼写是正确的——将其加入 docs/spelling_wordlist (请保持这个列表以字母顺序排列)。

    查看 本地化 Django 文档,如果你想帮助我们将文档翻译成其它语言。

    django-admin 手册页面

    Sphinx can generate a manual page for the 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 .