编写文档
我们极其重视文档的一致性和可读性。毕竟,Django 是在需要快速发布新闻的环境下开发的!所以,我们像对待我们的代码一样对待我们的文档:我们期望尽可能频繁地更新它。
一般来说,文档会在以下两种情况时更新:
- 一般改进:通过更清晰的书写和更多示例,更正、修复文档错误,更好的解释功能。
- 新特性:自上一个版本发布后,添加到框架中的功能文档。
本节介绍文档作者如何以最有用和最不容易出错的方式修改文档。
获得原始文档
Django 文档可在 https://docs.djangoproject.com/ 以网页的形式阅读,但我们以一种更灵活的方式编辑它——一系列的文本文件。这些文件位于 Django 的每个发布分支的顶级目录 docs/
下。
如果你想修改文档,请先从源码仓库获取开发版的 Django (参见 安装开发版)。开发版拥有最新最好的文档,就像它拥有最新最好的代码一样。我们也会在最新发布分支上提交针对文档的修复和优化(取决于提交者)。这是因为让最新版本的文档保持最新和正确是非常有利的(参见 不同版本文档间的区别)。
开始使用 Sphinx
Django 的文档使用 Sphinx 文档系统——基于 docutils。基本思想是将轻量格式话的纯文本转化为 HTML,PDF 或其它任意输出格式。
To build the documentation locally, install Sphinx:
- $ pip install Sphinx
- ...\> pip install Sphinx
Then from the docs
directory, build the HTML:
- $ make html
- ...\> make.bat html
编写文档前,你需要阅读 reStructuredText 指引。
本地构建的文档的主题会和 docs.djangoproject.com 上的不同。没事!如果你修改后的文档在本地看起来没啥问题,那么在网站上也会没问题。
文档是如何组成
文档被分为以下几个类别:
- 教程 通过几步手把手的教学帮助读者创建一个小玩意。
教程的目的是帮助读者尽可能早地实现一些有用的东西,以便给他们带来信心。
Explain the nature of the problem we're solving, so that the readerunderstands what we're trying to achieve. Don't feel that you need to beginwith 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 andexplain afterwards.
- 主题指引 旨在在一个较高的层次介绍一个原则或主题。
链接至参考资料而不要重复它。使用示例时,不要不情愿解释对您而言非常基本的事物——它对别人而言可能需要解释。
提供背景信息有助于新人将主题和他们已知的东西联系起来。
- Reference guides contain technical reference for APIs.They describe the functioning of Django's internal machinery and instruct inits use.
Keep reference material tightly focused on the subject. Assume that thereader already understands the basic concepts involved but needs to know orbe reminded of how Django does it.
Reference guides aren't the place for general explanation. If you findyourself explaining basic concepts, you may want to move that material to atopic guide.
- How-to guides are recipes that take the reader throughsteps 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 internaldetails of how Django implements whatever is being discussed.
These guides are more advanced than tutorials and assume some knowledge abouthow Django works. Assume that the reader has followed the tutorials and don'thesitate to refer the reader back to the appropriate tutorial rather thanrepeat the same material.
书写格式
When using pronouns in reference to a hypothetical person, such as "a user witha session cookie", gender neutral pronouns (they/their/them) should be used.Instead of:
- 他或她……使用他们。
- him 或 her… 使用 them。
- 他的或她的……使用他们的。
- his 或 hers… 使用 theirs。
- himself 或 herself… 使用 themselves。
常用术语
以下是整个文档中常用术语的一些格式指南:
- 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)文档格式:
In section titles, capitalize only initial words and proper nouns.
Wrap the documentation at 80 characters wide, unless a code exampleis significantly less readable when split over two lines, or for anothergood reason.
The main thing to keep in mind as you write and edit docs is that themore 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, whichgreatly helps readers.
You can prefix the target with a ~
(that's a tilde) to get just the"last bit" of that path. So
will justdisplay a link with the title "auth".~django.contrib.auth
Use
intersphinx
to reference Python's and Sphinx'documentation.Add
.. code-block:: <lang>
to literal blocks so that they gethighlighted. Prefer relying on automatic highlighting simply using::
(two colons). This has the benefit that if the code contains some invalidsyntax, it won't be highlighted. Adding.. code-block:: python
, forexample, will force highlighting despite invalid syntax.Use these heading styles:
- ===
- One
- ===
- Two
- ===
- Three
- -----
- Four
- ~~~~
- Five
- ^^^^
Django-specific markup
Besides Sphinx's built-in markup, Django's docsdefine some extra description units:
- Settings:
- .. setting:: INSTALLED_APPS
To link to a setting, use
.INSTALLED_APPS
- Template tags:
- .. templatetag:: regroup
To link, use
.regroup
- Template filters:
- .. templatefilter:: linebreaksbr
To link, use
.linebreaksbr
- Field lookups (i.e.
Foo.objects.filter(bar__exact=whatever)
):
- .. fieldlookup:: exact
To link, use
.exact
django-admin
commands:
- .. django-admin:: migrate
To link, use
.migrate
django-admin
command-line options:
- .. django-admin-option:: --traceback
To link, use
(or omit command_name --traceback
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 documentingcommand-line examples involving django-admin.py
, manage.py
, python
,etc.). In the HTML documentation, it renders a two-tab UI, with one tab showinga 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 stillwrite 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 firstone will show:
- $ python manage.py shell
(No changes from what .. code-block:: console
would have rendered).
The second one will show:
- ...\> py manage.py shell
Documenting new features
Our policy for new features is:
All documentation of new features should be written in a way thatclearly designates the features are only available in the Djangodevelopment version. Assume documentation readers are using the latestrelease, not the development version.
Our preferred way for marking new features is by prefacing the features'documentation with: ".. versionadded:: X.Y
", followed by a mandatoryblank line and an optional description (indented).
General improvements, or other changes to the APIs that should be emphasizedshould use the ".. versionchanged:: X.Y
" directive (with the same formatas 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 havingto reflow, reindent, or edit the surrounding text. For example, instead ofputting the entire description of a new or changed feature in a block, dosomething 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 aversionadded
or versionchanged
block. Even inside a block, it's oftenredundant 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 aversionadded
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 anyindentation changes when the time comes.
Minimizing images
Optimize image compression where possible. For PNG files, use OptiPNG andAdvanceCOMP's advpng
:
- $ 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.
一个例子
For a quick example of how it all fits together, consider this hypotheticalexample:
- First, the
ref/settings.txt
document could have an overall layoutlike this:
- ========
- Settings
- ========
- ...
- .. _available-settings:
- Available settings
- ==================
- ...
- .. _deprecated-settings:
- Deprecated settings
- ===================
- ...
- Next, the
topics/settings.txt
document could contain something likethis:
- 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 doc
cross reference element when we want tolink to another document as a whole and the ref
element whenwe want to link to an arbitrary location in a document.
- Next, notice how the settings are annotated:
- .. 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 thesetting ADMINS
. This means any time I talk about ADMINS
,I can reference it using
.ADMINS
That's basically how everything fits together.
拼写检查
在提交文档前运行拼写检查是个不错的注意。你需要先装几个包:
- pyenchant (which requiresenchant)
- sphinxcontrib-spelling
Then from thedocs
directory, runmake 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), doone 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 thedjango-admin command. This is configured indocs/conf.py
. Unlike other documentation output, this man page should beincluded in the Django repository and the releases asdocs/man/django-admin.1
. There isn't a need to update this file whenupdating 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 thedocs
directory. The new man page will be written indocs/_build/man/django-admin.1
.