Django 文档可在 https://docs.djangoproject.com/ 以网页的形式阅读，但我们以一种更灵活的方式编辑它——一系列的文本文件。这些文件位于 Django 的每个发布分支的顶级目录
如果你想修改文档，请先从源码仓库获取开发版的 Django （参见 安装开发版）。开发版拥有最新最好的文档，就像它拥有最新最好的代码一样。我们也会在最新发布分支上提交针对文档的修复和优化（取决于提交者）。这是因为让最新版本的文档保持最新和正确是非常有利的（参见 不同版本文档间的区别）。
To build the documentation locally, install Sphinx:
$ pip install Sphinx
Then from the
docs directory, build the HTML:
$ make html
编写文档前，你需要阅读 reStructuredText 指引。
本地构建的文档的主题会和 docs.djangoproject.com 上的不同。没事！如果你修改后的文档在本地看起来没啥问题，那么在网站上也会没问题。
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.
How-to guides 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:
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.
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".
intersphinx to reference Python's and Sphinx'
.. 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.
Use these heading styles:
=== One === Two === Three ----- Four ~~~~ Five ^^^^
:rfc: to reference RFC and and try to link to the
relevant section if possible. For example, use
:rfc:`Custom link text <2324#section-2.3.2>`.
Besides Sphinx's built-in markup, Django's docs define some extra description units:
.. setting:: INSTALLED_APPS
To link to a setting, use
.. templatetag:: regroup
To link, use
.. templatefilter:: linebreaksbr
To link, use
Field lookups (i.e.
.. fieldlookup:: exact
To link, use
.. django-admin:: migrate
To link, use
django-admin command-line options:
.. django-admin-option:: --traceback
To link, use
:option:`command_name --traceback` (or omit
for the options shared by all commands like
Links to Trac tickets (typically reserved for patch release notes):
Django's documentation uses a custom
console directive for documenting
command-line examples involving
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:
.. code-block:: consoledirective.
'/'as filesystem path components separator, etc.)
The example above will render a code example block with two tabs. The first one 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
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
versionadded mentioned above.
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
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.
Optimize image compression where possible. For PNG files, use OptiPNG and
$ 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 hypothetical example:
ref/settings.txt document could have an overall layout
======== Settings ======== ... .. _available-settings: Available settings ================== ... .. _deprecated-settings: Deprecated settings =================== ...
topics/settings.txt document could contain something like
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 to
link to another document as a whole and the
ref element when
we 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', '[email protected]'), ('Mary', '[email protected]')] 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
ADMINS. This means any time I talk about
I can reference it using
That's basically how everything fits together.
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
If you encounter false-positives (error output that actually is correct), do one of the following:
查看 本地化 Django 文档，如果你想帮助我们将文档翻译成其它语言。
Sphinx can generate a manual page for the
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