{"title":"编写文档","version":"4.0","locale":"zh-hans","docname":"internals/contributing/writing-documentation","url":"/zh-hans/4.0/internals/contributing/writing-documentation/","canonical":"https://djangodocs.dev/zh-hans/4.0/internals/contributing/writing-documentation/","summary":"我们极其重视文档的一致性和可读性。毕竟，Django 是在需要快速发布新闻的环境下开发的！所以，我们像对待我们的代码一样对待我们的文档：我们期望尽可能频繁地更新它。 一般来说，文档会在以下两种情况时更新： 一般改进：通过更清晰的书写和更多示例，更正、修复文档错误，更好的解释功能。…","html":"<h1>编写文档<a class=\"heading-anchor\" href=\"#writing-documentation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>我们极其重视文档的一致性和可读性。毕竟，Django 是在需要快速发布新闻的环境下开发的！所以，我们像对待我们的代码一样对待我们的文档：我们期望尽可能频繁地更新它。</p>\n<p>一般来说，文档会在以下两种情况时更新：</p>\n<ul class=\"simple\">\n<li><p>一般改进：通过更清晰的书写和更多示例，更正、修复文档错误，更好的解释功能。</p></li>\n<li><p>新特性：自上一个版本发布后，添加到框架中的功能文档。</p></li>\n</ul>\n<p>本节介绍文档作者如何以最有用和最不容易出错的方式修改文档。</p>\n<section id=\"getting-the-raw-documentation\">\n<h2>获得原始文档<a class=\"heading-anchor\" href=\"#getting-the-raw-documentation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django 文档可在 <a class=\"reference external\" href=\"https://docs.djangoproject.com/\">https://docs.djangoproject.com/</a> 以网页的形式阅读，但我们以一种更灵活的方式编辑它——一系列的文本文件。这些文件位于  Django 的每个发布分支的顶级目录 <code class=\"docutils literal notranslate\"><span class=\"pre\">docs/</span></code> 下。</p>\n<p>If you'd like to start contributing to our docs, get the development version of\nDjango from the source code repository\n(see <a class=\"reference internal\" href=\"/zh-hans/4.0/topics/install/#installing-development-version\"><span class=\"std std-ref\">安装开发版本</span></a>). The development version has the\nlatest-and-greatest documentation, just as it has latest-and-greatest code.\nWe also backport documentation fixes and improvements, at the discretion of the\nmerger, to the last release branch. That's because it's highly advantageous to\nhave the docs for the last release be up-to-date and correct (see\n<a class=\"reference internal\" href=\"/zh-hans/4.0/intro/whatsnext/#differences-between-doc-versions\"><span class=\"std std-ref\">版本之间的差异</span></a>).</p>\n</section>\n<section id=\"getting-started-with-sphinx\">\n<h2>开始使用 Sphinx<a class=\"heading-anchor\" href=\"#getting-started-with-sphinx\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django  的文档使用 <a class=\"reference external\" href=\"https://www.sphinx-doc.org/\">Sphinx</a> 文档系统——基于 <a class=\"reference external\" href=\"https://docutils.sourceforge.io/\">docutils</a>。基本思想是将轻量格式话的纯文本转化为 HTML，PDF 或其它任意输出格式。</p>\n<p>要在本地构建文档，请安装 Sphinx：</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-0-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>-m<span class=\"w\"> </span>pip<span class=\"w\"> </span>install<span class=\"w\"> </span>Sphinx\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-0-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py -m pip install Sphinx\n</code></pre></div></div></div>\n<p>然后从 <code class=\"docutils literal notranslate\"><span class=\"pre\">docs</span></code> 目录下，编译 HTML：</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-1-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>make<span class=\"w\"> </span>html\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-1-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> make.bat html\n</code></pre></div></div></div>\n<p>编写文档前，你需要阅读 <a class=\"reference external\" href=\"https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html#rst-index\" title=\"(in Sphinx v9.1.1)\"><span class=\"xref std std-ref\">reStructuredText 指引</span></a>。</p>\n<p>Your locally-built documentation will be themed differently than the\ndocumentation at <a class=\"reference external\" href=\"https://docs.djangoproject.com/\">docs.djangoproject.com</a>.\nThis is OK! If your changes look good on your local machine, they'll look good\non the website.</p>\n</section>\n<section id=\"how-the-documentation-is-organized\">\n<h2>文档是如何组成<a class=\"heading-anchor\" href=\"#how-the-documentation-is-organized\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>文档被分为以下几个类别：</p>\n<ul>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/4.0/intro/\"><span class=\"doc\">教程</span></a> 通过几步手把手的教学帮助读者创建一个小玩意。</p>\n<p>教程的目的是帮助读者尽可能早地实现一些有用的东西，以便给他们带来信心。</p>\n<p>Explain the nature of the problem we're solving, so that the reader\nunderstands what we're trying to achieve. Don't feel that you need to begin\nwith explanations of how things work - what matters is what the reader does,\nnot what you explain. It can be helpful to refer back to what you've done and\nexplain afterward.</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/4.0/topics/\"><span class=\"doc\">主题指引</span></a> 旨在在一个较高的层次介绍一个原则或主题。</p>\n<p>链接至参考资料而不要重复它。使用示例时，不要不情愿解释对您而言非常基本的事物——它对别人而言可能需要解释。</p>\n<p>提供背景信息有助于新人将主题和他们已知的东西联系起来。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/4.0/ref/\"><span class=\"doc\">参考指南</span></a> 包含 API 的技术参考。它们描述了 Django 的内部机制的运作，并指导其使用。</p>\n<p>让参考资料紧紧围绕着主题。假设读者已经理解了所涉及的基本概念，但需要知道或被提醒 Django 是如何做到的。</p>\n<p>参考指南并不是进行一般性解释的地方。如果你发现自己在解释基本概念，你可能想把这些材料移到主题指南中。</p>\n</li>\n<li><p><a class=\"reference internal\" href=\"/zh-hans/4.0/howto/\"><span class=\"doc\">操作指南</span></a> 是带领读者完成关键科目步骤的方法。</p>\n<p>在指南中最重要的是用户想要实现什么。一个指南应该始终以结果为导向，而不是专注于 Django 如何实现所讨论的内部细节。</p>\n<p>这些指南比教程更高级，并假定有一些关于 Django 如何工作的知识。假设读者已经学习了教程，并且毫不犹豫地让读者回到相应的教程，而不是重复同样的材料。</p>\n</li>\n</ul>\n</section>\n<section id=\"writing-style\">\n<h2>书写格式<a class=\"heading-anchor\" href=\"#writing-style\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>当使用代词指代一个假设的人时，如 “a user with a session cookie”，应使用性别中性的代词（they/their/them）。而不是：</p>\n<ul class=\"simple\">\n<li><p>he 或 she……使用 they。</p></li>\n<li><p>him 或 her... 使用 them。</p></li>\n<li><p>his 或 her……使用 their。</p></li>\n<li><p>his 或 hers... 使用 theirs。</p></li>\n<li><p>himself 或 herself... 使用 themselves。</p></li>\n</ul>\n<p>尽量避免使用将任务或操作的难度降到最低的词语，如 “easily”、“simply”、“just”、“merely”、“straightforward” 等等。人们的经验可能与你的期望不符，当他们发现某个步骤并不像暗示的那样 “straightforward” 或 “simple” 时，可能会感到沮丧。</p>\n</section>\n<section id=\"commonly-used-terms\">\n<h2>常用术语<a class=\"heading-anchor\" href=\"#commonly-used-terms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>以下是整个文档中常用术语的一些格式指南：</p>\n<ul class=\"simple\">\n<li><p><strong>Django</strong> -- 当提及该框架时，大写 Django。它仅在 Python 代码中和 djangoproject.com 徽标中使用小写字母。</p></li>\n<li><p><strong>email</strong> -- 无连字符。</p></li>\n<li><p><strong>HTTP</strong> -- the expected pronunciation is &quot;Aitch Tee Tee Pee&quot; and therefore\nshould be preceded by &quot;an&quot; and not &quot;a&quot;.</p></li>\n<li><p><strong>MySQL</strong>, <strong>PostgreSQL</strong>, <strong>SQLite</strong></p></li>\n<li><p><strong>SQL</strong> -- 当提及 SQL 时，预期的发音应该是 “Ess Queue Ell” 而不是 “sequel”。因此，在诸如 “Returns an SQL expression” 之类的短语中，“SQL” 前应该使用 “an” 而不是 “a”。</p></li>\n<li><p><strong>Python</strong> -- 当提及该语言时大写。</p></li>\n<li><p><strong>realize</strong>, <strong>customize</strong>, <strong>initialize</strong>, etc. -- 使用美式的 “ize” 后缀，而不是 “ise”。</p></li>\n<li><p><strong>subclass</strong> -- 它是一个没有连字符的单个单词，既作为动词（“子类模型”）又作为名词（“创建子类”）。</p></li>\n<li><p><strong>the web</strong>, <strong>web framework</strong> -- it's not capitalized.</p></li>\n<li><p><strong>website</strong> -- 用一个单词表示，不大写。</p></li>\n</ul>\n</section>\n<section id=\"django-specific-terminology\">\n<h2>Django 专用术语<a class=\"heading-anchor\" href=\"#django-specific-terminology\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<ul class=\"simple\">\n<li><p><strong>model</strong> -- 它不是大写的。</p></li>\n<li><p><strong>template</strong> -- 它不是大写的。</p></li>\n<li><p><strong>URLconf</strong> -- 使用了三个大写字母，在 “conf” 之前没有空格。</p></li>\n<li><p><strong>view</strong> -- 它不是大写的。</p></li>\n</ul>\n</section>\n<section id=\"guidelines-for-restructuredtext-files\">\n<h2>reStructuredText 文件语法指南<a class=\"heading-anchor\" href=\"#guidelines-for-restructuredtext-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>这些准则规定了我们的 reST（reStructuredText）文档格式：</p>\n<ul>\n<li><p>在部分标题中，仅将首字母和专有名词大写。</p></li>\n<li><p>将文档以 80 个字符宽换行，除非一个代码例子被分割成两行时可读性明显降低，或者有其他好的理由。</p></li>\n<li><p>在编写和编辑文档时要记住的主要事情是，可以添加的语义标记越多越好。 所以：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>Add ``django.contrib.auth`` to your ``INSTALLED_APPS``...\n</code></pre></div>\n<p>远没有：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>Add :mod:`django.contrib.auth` to your :setting:`INSTALLED_APPS`...\n</code></pre></div>\n<p>这是因为 Sphinx 将为后者生成适当的链接，这对读者有很大帮助。</p>\n<p>你可以在目标前加一个 <code class=\"docutils literal notranslate\"><span class=\"pre\">~</span></code> （那是一个波浪号）来获得该路径的 “最后一点”。所以 <code class=\"docutils literal notranslate\"><span class=\"pre\">:mod:`~django.contrib.auth</span></code> 将显示一个标题为 “auth” 的链接。</p>\n</li>\n<li><p>使用 <a class=\"reference external\" href=\"https://www.sphinx-doc.org/en/master/usage/extensions/intersphinx.html#module-sphinx.ext.intersphinx\" title=\"(in Sphinx v9.1.1)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">intersphinx</span></code></a> 来引用 Python 和 Sphinx 的文档。</p></li>\n<li><p>在文字块中加入 <code class=\"docutils literal notranslate\"><span class=\"pre\">..</span> <span class=\"pre\">code-block::</span> <span class=\"pre\">&lt;lang&gt;</span></code>，使其得到高亮。更倾向于使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">::</span></code> （两个冒号）来自动突出显示。这样做的好处是，如果代码中包含一些无效的语法，它就不会被高亮显示。例如，添加 <code class=\"docutils literal notranslate\"><span class=\"pre\">..</span> <span class=\"pre\">code-block::</span> <span class=\"pre\">python</span></code>，就可以在无效的语法中强制高亮显示。</p></li>\n<li><p>为了提高可读性，使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">..</span> <span class=\"pre\">admonition::</span> <span class=\"pre\">Descriptive</span> <span class=\"pre\">title</span></code> 而不是 <code class=\"docutils literal notranslate\"><span class=\"pre\">..</span> <span class=\"pre\">note::</span></code>。尽量少使用这些方框。</p></li>\n<li><p>使用这些标题样式：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"o\">===</span>\n<span class=\"n\">One</span>\n<span class=\"o\">===</span>\n\n<span class=\"n\">Two</span>\n<span class=\"o\">===</span>\n\n<span class=\"n\">Three</span>\n<span class=\"o\">-----</span>\n\n<span class=\"n\">Four</span>\n<span class=\"o\">~~~~</span>\n\n<span class=\"n\">Five</span>\n<span class=\"o\">^^^^</span>\n</code></pre></div>\n</li>\n<li><p>使用 <a class=\"reference external\" href=\"https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#role-rfc\" title=\"(in Sphinx v9.1.1)\"><code class=\"xref rst rst-role docutils literal notranslate\"><span class=\"pre\">:rfc:</span></code></a> 来引用 RFC，如果可能，尽量链接到相关章节。例如，使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">:rfc:`2324#section-2.3.2`</span></code> 或 <code class=\"docutils literal notranslate\"><span class=\"pre\">:rfc:`Custom</span> <span class=\"pre\">link</span> <span class=\"pre\">text</span> <span class=\"pre\">&lt;2324#section-2.3.2&gt;`</span></code>。</p></li>\n<li><p>使用 <a class=\"reference external\" href=\"https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#role-pep\" title=\"(in Sphinx v9.1.1)\"><code class=\"xref rst rst-role docutils literal notranslate\"><span class=\"pre\">:pep:</span></code></a> 来引用 Python 增强建议（PEP），如果可能的话，尽量链接到相关章节。例如，使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">:pep:`20#easter-egg`</span></code> 或 <code class=\"docutils literal notranslate\"><span class=\"pre\">:pep:`Easter</span> <span class=\"pre\">Egg</span> <span class=\"pre\">&lt;20#easter-egg&gt;`</span></code>。</p></li>\n<li><p>使用 <a class=\"reference external\" href=\"https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html#role-mimetype\" title=\"(in Sphinx v9.1.1)\"><code class=\"xref rst rst-role docutils literal notranslate\"><span class=\"pre\">:mimetype:</span></code></a> 来指代一个 MIME 类型，除非在代码示例中引用了这个值。</p></li>\n<li><p>使用 <a class=\"reference external\" href=\"https://www.sphinx-doc.org/en/master/usage/referencing.html#role-envvar\" title=\"(in Sphinx v9.1.1)\"><code class=\"xref rst rst-role docutils literal notranslate\"><span class=\"pre\">:envvar:</span></code></a> 来指代一个环境变量。你可能还需要使用 <a class=\"reference external\" href=\"https://www.sphinx-doc.org/en/master/usage/domains/standard.html#directive-envvar\" title=\"(in Sphinx v9.1.1)\"><code class=\"xref rst rst-dir docutils literal notranslate\"><span class=\"pre\">...envvar::</span></code></a> 来定义一个对该环境变量的文档的引用。</p></li>\n</ul>\n</section>\n<section id=\"django-specific-markup\">\n<h2>Django 特有的标记<a class=\"heading-anchor\" href=\"#django-specific-markup\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>除了 <a class=\"reference external\" href=\"https://www.sphinx-doc.org/en/master/usage/restructuredtext/index.html#rst-index\" title=\"(in Sphinx v9.1.1)\"><span class=\"xref std std-ref\">Sphinx 的内置标记</span></a>，Django 的文档定义了一些额外的描述单元：</p>\n<ul>\n<li><p>配置：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"o\">..</span> <span class=\"n\">setting</span><span class=\"p\">::</span> <span class=\"n\">INSTALLED_APPS</span>\n</code></pre></div>\n<p>为了连接配置，请使用配置 <code class=\"docutils literal notranslate\"><span class=\"pre\">:setting:`INSTALLED_APPS`</span></code>。</p>\n</li>\n<li><p>模板标签：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"o\">..</span> <span class=\"n\">templatetag</span><span class=\"p\">::</span> <span class=\"n\">regroup</span>\n</code></pre></div>\n<p>为了链接，请使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">:ttag:`regroup`</span></code>。</p>\n</li>\n<li><p>模板过滤器：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"o\">..</span> <span class=\"n\">templatefilter</span><span class=\"p\">::</span> <span class=\"n\">linebreaksbr</span>\n</code></pre></div>\n<p>为了链接，请使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">:tfilter:`linebreaksbr`</span></code> 。</p>\n</li>\n<li><p>字段查询（例如 <code class=\"docutils literal notranslate\"><span class=\"pre\">Foo.objects.filter(bar__exact=whatever)</span></code>）：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"o\">..</span> <span class=\"n\">fieldlookup</span><span class=\"p\">::</span> <span class=\"n\">exact</span>\n</code></pre></div>\n<p>为了链接，请使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">:lookup:`exact`</span></code>。</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">django-admin</span></code> 管理员命令：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"o\">..</span> <span class=\"n\">django</span><span class=\"o\">-</span><span class=\"n\">admin</span><span class=\"p\">::</span> <span class=\"n\">migrate</span>\n</code></pre></div>\n<p>为了链接，请使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">:djadmin:`migrate`</span></code>。</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">django-admin</span></code> 管理员命令行选项：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"o\">..</span> <span class=\"n\">django</span><span class=\"o\">-</span><span class=\"n\">admin</span><span class=\"o\">-</span><span class=\"n\">option</span><span class=\"p\">::</span> <span class=\"o\">--</span><span class=\"n\">traceback</span>\n</code></pre></div>\n<p>为了链接，请使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">:option:`command_name</span> <span class=\"pre\">--trackback</span></code> （或者省略 <code class=\"docutils literal notranslate\"><span class=\"pre\">command_name</span></code>，用于所有命令共享的选项，如 <code class=\"docutils literal notranslate\"><span class=\"pre\">--verbosity</span></code>）。</p>\n</li>\n<li><p>链接到追踪工单（通常为补丁发行说明保留）：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>:ticket:`12345`\n</code></pre></div>\n</li>\n</ul>\n<p>Django 的文档使用一个自定义的 <code class=\"docutils literal notranslate\"><span class=\"pre\">console</span></code> 指令，用于记录涉及 <code class=\"docutils literal notranslate\"><span class=\"pre\">django-admin</span></code>、<code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span></code>、<code class=\"docutils literal notranslate\"><span class=\"pre\">python</span></code> 等的命令行实例。在 HTML 文档中，它渲染了一个双选项卡 UI，其中一个选项卡显示 Unix 风格的命令提示符，第二个选项卡显示 Windows 提示符。</p>\n<p>例如，你可以替换这个片段：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>use this command:\n\n.. code-block:: console\n\n    $ python manage.py shell\n</code></pre></div>\n<p>为这个：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>use this command:\n\n.. console::\n\n    $ python manage.py shell\n</code></pre></div>\n<p>请注意两件事：</p>\n<ul class=\"simple\">\n<li><p>你通常会替换出现的 <code class=\"docutils literal notranslate\"><span class=\"pre\">..</span> <span class=\"pre\">code-block::</span> <span class=\"pre\">console</span></code> 指令。</p></li>\n<li><p>你不需要改变代码例子的实际内容。你仍然假设 Unix-y 环境来编写它（即一个 <code class=\"docutils literal notranslate\"><span class=\"pre\">'$'</span></code> 提示符号，<code class=\"docutils literal notranslate\"><span class=\"pre\">'/'</span></code> 作为文件系统路径组件分隔符等等）。</p></li>\n</ul>\n<p>上面的例子将呈现一个有两个选项卡的代码示例块。第一个将显示：</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>shell\n</code></pre></div>\n<p>与 <code class=\"docutils literal notranslate\"><span class=\"pre\">..</span> <span class=\"pre\">code-block::</span> <span class=\"pre\">console</span></code> 所呈现的内容相比没有变化）。</p>\n<p>第二个将显示：</p>\n<div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows code\"><code><span class=\"gp\">...\\&gt;</span> py manage.py shell\n</code></pre></div>\n</section>\n<section id=\"documenting-new-features\">\n<span id=\"id3\"></span><h2>记录新功能<a class=\"heading-anchor\" href=\"#documenting-new-features\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>我们对新功能的政策是：</p>\n<blockquote>\n<div><p>所有关于新功能的文档都应该以明确指定该功能只在 Django 开发版本中可用的方式来编写。假设文档读者使用的是最新版本，而不是开发版本。</p>\n</div></blockquote>\n<p>我们首选的标记新特性的方法是在特性的文档前加上。&quot;<code class=\"docutils literal notranslate\"><span class=\"pre\">..</span> <span class=\"pre\">versionadded::</span> <span class=\"pre\">X.Y</span></code>&quot;，后面是一个强制性的空行和一个可选的描述（缩进）。</p>\n<p>常规改进或应强调的其他 API 更改应使用 &quot;<code class=\"docutils literal notranslate\"><span class=\"pre\">..</span> <span class=\"pre\">versionchanged::</span> <span class=\"pre\">X.Y</span></code>&quot; 指令（与上述 <code class=\"docutils literal notranslate\"><span class=\"pre\">versionadded</span></code> 相同。</p>\n<p>这些 <code class=\"docutils literal notranslate\"><span class=\"pre\">versionadded</span></code> 和 <code class=\"docutils literal notranslate\"><span class=\"pre\">versionchanged</span></code> 块应该是 “自包含的”。换句话说，由于我们只在两个版本中保留这些注释，所以最好能够删除注解及其内容，而不必对周围的文本进行重写、重新缩进或编辑。例如，与其把一个新的或改变了的功能的全部描述放在一个块中，不如这样做：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>.. class:: Author(first_name, last_name, middle_name=None)\n\n    A person who writes books.\n\n    ``first_name`` is ...\n\n    ...\n\n    ``middle_name`` is ...\n\n    .. versionchanged:: A.B\n\n        The ``middle_name`` argument was added.\n</code></pre></div>\n<p>把修改后的注解说明放在一个章节的底部，而不是顶部。</p>\n<p>另外，避免在 <code class=\"docutils literal notranslate\"><span class=\"pre\">versionadded</span></code> 或 <code class=\"docutils literal notranslate\"><span class=\"pre\">versionchanged</span></code> 块之外提及 Django 的特定版本。即使在代码块中，这样做也是多余的，因为这些注解分别呈现为 &quot;New in Django A.B:&quot; 和 &quot;Changed in Django A.B&quot;。</p>\n<p>如果增加了一个函数、属性等，使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">versionadded</span></code> 注解也是可以的，像这样：</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"o\">..</span> <span class=\"n\">attribute</span><span class=\"p\">::</span> <span class=\"n\">Author</span><span class=\"o\">.</span><span class=\"n\">middle_name</span>\n\n    <span class=\"o\">..</span> <span class=\"n\">versionadded</span><span class=\"p\">::</span> <span class=\"n\">A</span><span class=\"o\">.</span><span class=\"n\">B</span>\n\n    <span class=\"n\">An</span> <span class=\"n\">author</span><span class=\"s1\">&#39;s middle name.</span>\n</code></pre></div>\n<p>我们可以删除 <code class=\"docutils literal notranslate\"><span class=\"pre\">..</span> <span class=\"pre\">versionadded::</span> <span class=\"pre\">A.B</span></code> 注解，而不需要在时间上做任何缩进的改变。</p>\n</section>\n<section id=\"minimizing-images\">\n<h2>最小化图像<a class=\"heading-anchor\" href=\"#minimizing-images\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>尽可能地优化图像压缩。对于 PNG 文件，使用 OptiPNG 和 AdvanceCOMP 的 <code class=\"docutils literal notranslate\"><span class=\"pre\">advpng</span></code>：</p>\n<div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span><span class=\"nb\">cd</span><span class=\"w\"> </span>docs\n<span class=\"gp\">$ </span>optipng<span class=\"w\"> </span>-o7<span class=\"w\"> </span>-zm1-9<span class=\"w\"> </span>-i0<span class=\"w\"> </span>-strip<span class=\"w\"> </span>all<span class=\"w\"> </span><span class=\"sb\">`</span>find<span class=\"w\"> </span>.<span class=\"w\"> </span>-type<span class=\"w\"> </span>f<span class=\"w\"> </span>-not<span class=\"w\"> </span>-path<span class=\"w\"> </span><span class=\"s2\">&quot;./_build/*&quot;</span><span class=\"w\"> </span>-name<span class=\"w\"> </span><span class=\"s2\">&quot;*.png&quot;</span><span class=\"sb\">`</span>\n<span class=\"gp\">$ </span>advpng<span class=\"w\"> </span>-z4<span class=\"w\"> </span><span class=\"sb\">`</span>find<span class=\"w\"> </span>.<span class=\"w\"> </span>-type<span class=\"w\"> </span>f<span class=\"w\"> </span>-not<span class=\"w\"> </span>-path<span class=\"w\"> </span><span class=\"s2\">&quot;./_build/*&quot;</span><span class=\"w\"> </span>-name<span class=\"w\"> </span><span class=\"s2\">&quot;*.png&quot;</span><span class=\"sb\">`</span>\n</code></pre></div>\n<p>This is based on OptiPNG version 0.7.5. Older versions may complain about the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">-strip</span> <span class=\"pre\">all</span></code> option being lossy.</p>\n</section>\n<section id=\"an-example\">\n<h2>一个例子<a class=\"heading-anchor\" href=\"#an-example\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>有关如何将它们组合在一起的快速示例，请考虑以下假设示例：</p>\n<ul>\n<li><p>首先， <code class=\"docutils literal notranslate\"><span class=\"pre\">ref/settings.txt</span></code> 配置文件可能具有如下总体布局：</p>\n<div class=\"code-block\" data-language=\"rst\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Rst</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Rst code\"><code><span class=\"gh\">========</span>\n<span class=\"gh\">Settings</span>\n<span class=\"gh\">========</span>\n\n<span class=\"c\">...</span>\n\n<span class=\"p\">..</span> <span class=\"nt\">_available-settings:</span>\n\n<span class=\"gh\">Available settings</span>\n<span class=\"gh\">==================</span>\n\n<span class=\"c\">...</span>\n\n<span class=\"p\">..</span> <span class=\"nt\">_deprecated-settings:</span>\n\n<span class=\"gh\">Deprecated settings</span>\n<span class=\"gh\">===================</span>\n\n<span class=\"c\">...</span>\n</code></pre></div>\n</li>\n<li><p>接下来， <code class=\"docutils literal notranslate\"><span class=\"pre\">topics/settings.txt</span></code> 配置文档可能包含以下内容：</p>\n<div class=\"code-block\" data-language=\"rst\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Rst</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Rst code\"><code>You can access a :ref:`listing of all available settings\n<span class=\"nt\">&lt;available-settings&gt;</span>`. For a list of deprecated settings see\n<span class=\"na\">:ref:</span><span class=\"nv\">`deprecated-settings`</span>.\n\nYou can find both in the :doc:`settings reference document\n<span class=\"nt\">&lt;/ref/settings&gt;</span>`.\n</code></pre></div>\n<p>当我们想链接到另一个文档时，我们使用 <a class=\"reference external\" href=\"https://www.sphinx-doc.org/en/master/usage/referencing.html#role-doc\" title=\"(in Sphinx v9.1.1)\"><code class=\"xref rst rst-role docutils literal notranslate\"><span class=\"pre\">doc</span></code></a> 交叉引用元素；当我们想链接到文档中的任意的位置时，请使用 <a class=\"reference external\" href=\"https://www.sphinx-doc.org/en/master/usage/referencing.html#role-ref\" title=\"(in Sphinx v9.1.1)\"><code class=\"xref rst rst-role docutils literal notranslate\"><span class=\"pre\">ref</span></code></a> 元素。</p>\n</li>\n<li><p>接下来，请注意配置的注解方式：</p>\n<div class=\"code-block\" data-language=\"rst\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Rst</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Rst code\"><code><span class=\"p\">..</span> <span class=\"ow\">setting</span><span class=\"p\">::</span> ADMINS\n\n<span class=\"gh\">ADMINS</span>\n<span class=\"gh\">======</span>\n\nDefault: <span class=\"s\">``[]``</span> (Empty list)\n\nA list of all the people who get code error notifications. When\n<span class=\"s\">``DEBUG=False``</span> and a view raises an exception, Django will email these people\nwith the full exception information. Each member of the list should be a tuple\nof (Full name, email address). Example<span class=\"se\">::</span>\n\n<span class=\"s\">    [(&#39;John&#39;, &#39;john@example.com&#39;), (&#39;Mary&#39;, &#39;mary@example.com&#39;)]</span>\n\nNote that Django will email <span class=\"ge\">*all*</span> of these people whenever an error happens.\nSee <span class=\"na\">:doc:</span><span class=\"nv\">`/howto/error-reporting`</span> for more information.\n</code></pre></div>\n<p>这标志着下面的标题是配置 <code class=\"docutils literal notranslate\"><span class=\"pre\">ADMINS</span></code> 的 “标准” 目标。这意味着当我谈到 <code class=\"docutils literal notranslate\"><span class=\"pre\">ADMINS</span></code> 时，我可以用 <code class=\"docutils literal notranslate\"><span class=\"pre\">:setting:`ADMINS`</span></code> 来引用它。</p>\n</li>\n</ul>\n<p>基本上，这就是所有东西融合在一起的方式。</p>\n</section>\n<section id=\"spelling-check\">\n<span id=\"documentation-spelling-check\"></span><h2>拼写检查<a class=\"heading-anchor\" href=\"#spelling-check\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>在你提交你的文档之前，运行拼写检查器是个好主意。你需要先安装 <a class=\"reference external\" href=\"https://pypi.org/project/sphinxcontrib-spelling/\">sphinxcontrib-spelling</a> 。然后从 <code class=\"docutils literal notranslate\"><span class=\"pre\">docs</span></code> 目录下，运行 <code class=\"docutils literal notranslate\"><span class=\"pre\">make</span> <span class=\"pre\">spelling</span></code>。错误的词（如果有的话）以及它们出现的文件和行号将被保存到 <code class=\"docutils literal notranslate\"><span class=\"pre\">_build/spelling/output.txt</span></code>。</p>\n<p>如果你遇到假阳性的情况（错误输出实际上是正确的），请采取以下措施之一：</p>\n<ul class=\"simple\">\n<li><p>用重音（`）包围内联代码或品牌／技术名称。</p></li>\n<li><p>查找拼写检查程序发现的同义词。</p></li>\n<li><p>如果，只是如果，你确定你的单词拼写是正确的——将其加入 <code class=\"docutils literal notranslate\"><span class=\"pre\">docs/spelling_wordlist</span></code> （请保持这个列表以字母顺序排列）。</p></li>\n</ul>\n</section>\n<section id=\"link-check\">\n<span id=\"documentation-link-check\"></span><h2>Link check<a class=\"heading-anchor\" href=\"#link-check\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Links in documentation can become broken or changed such that they are no\nlonger the canonical link. Sphinx provides a builder that can check whether the\nlinks in the documentation are working. From the <code class=\"docutils literal notranslate\"><span class=\"pre\">docs</span></code> directory, run <code class=\"docutils literal notranslate\"><span class=\"pre\">make</span>\n<span class=\"pre\">linkcheck</span></code>. Output is printed to the terminal, but can also be found in\n<code class=\"docutils literal notranslate\"><span class=\"pre\">_build/linkcheck/output.txt</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">_build/linkcheck/output.json</span></code>.</p>\n<p>Entries that have a status of &quot;working&quot; are fine, those that are &quot;unchecked&quot; or\n&quot;ignored&quot; have been skipped because they either cannot be checked or have\nmatched ignore rules in the configuration.</p>\n<p>Entries that have a status of &quot;broken&quot; need to be fixed. Those that have a\nstatus of &quot;redirected&quot; may need to be updated to point to the canonical\nlocation, e.g. the scheme has changed <code class=\"docutils literal notranslate\"><span class=\"pre\">http://</span></code> → <code class=\"docutils literal notranslate\"><span class=\"pre\">https://</span></code>. In certain\ncases, we do not want to update a &quot;redirected&quot; link, e.g. a rewrite to always\npoint to the latest or stable version of documentation, e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">/en/stable/</span></code> →\n<code class=\"docutils literal notranslate\"><span class=\"pre\">/en/3.2/</span></code>.</p>\n</section>\n<section id=\"translating-documentation\">\n<h2>翻译文档<a class=\"heading-anchor\" href=\"#translating-documentation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>查看 <a class=\"reference internal\" href=\"/zh-hans/4.0/internals/contributing/localizing/#translating-documentation\"><span class=\"std std-ref\">本地化 Django 文档</span></a>，如果你想帮助我们将文档翻译成其它语言。</p>\n</section>\n<section id=\"django-admin-man-page\">\n<span id=\"django-admin-manpage\"></span><h2><code class=\"docutils literal notranslate\"><span class=\"pre\">django-admin</span></code> 手册页面<a class=\"heading-anchor\" href=\"#django-admin-man-page\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Sphinx 可以为 <a class=\"reference internal\" href=\"/zh-hans/4.0/ref/django-admin/\"><span class=\"doc\">django-admin</span></a> 命令生成一个手册页。这是在 <code class=\"docutils literal notranslate\"><span class=\"pre\">docs/conf.py</span></code> 中配置的。与其他文档输出不同，这个手册页应该包含在 Django 仓库和版本中，作为 <code class=\"docutils literal notranslate\"><span class=\"pre\">docs/man/django-admin.1</span></code>。在更新文档时不需要更新这个文件，因为它作为发行过程的一部分被更新一次。</p>\n<p>要生成更新版本的手册，请在 <code class=\"docutils literal notranslate\"><span class=\"pre\">docs</span></code> 目录下运行 <code class=\"docutils literal notranslate\"><span class=\"pre\">make</span> <span class=\"pre\">man</span></code>。新的手册页将写在 <code class=\"docutils literal notranslate\"><span class=\"pre\">docs/_build/man/django-admin.1</span></code>。</p>\n</section>","rootId":"writing-documentation","toc":[{"title":"获得原始文档","anchor":"getting-the-raw-documentation","children":[]},{"title":"开始使用 Sphinx","anchor":"getting-started-with-sphinx","children":[]},{"title":"文档是如何组成","anchor":"how-the-documentation-is-organized","children":[]},{"title":"书写格式","anchor":"writing-style","children":[]},{"title":"常用术语","anchor":"commonly-used-terms","children":[]},{"title":"Django 专用术语","anchor":"django-specific-terminology","children":[]},{"title":"reStructuredText 文件语法指南","anchor":"guidelines-for-restructuredtext-files","children":[]},{"title":"Django 特有的标记","anchor":"django-specific-markup","children":[]},{"title":"记录新功能","anchor":"documenting-new-features","children":[]},{"title":"最小化图像","anchor":"minimizing-images","children":[]},{"title":"一个例子","anchor":"an-example","children":[]},{"title":"拼写检查","anchor":"spelling-check","children":[]},{"title":"Link check","anchor":"link-check","children":[]},{"title":"翻译文档","anchor":"translating-documentation","children":[]},{"title":"django-admin 手册页面","anchor":"django-admin-man-page","children":[]}],"breadcrumbs":[{"docname":"internals/index","title":"Django internals","url":"/zh-hans/4.0/internals/"},{"docname":"internals/contributing/index","title":"为 Django 做贡献","url":"/zh-hans/4.0/internals/contributing/"}],"prev":{"docname":"internals/contributing/writing-code/javascript","title":"JavaScript","url":"/zh-hans/4.0/internals/contributing/writing-code/javascript/"},"next":{"docname":"internals/contributing/localizing","title":"使 Django 本地化","url":"/zh-hans/4.0/internals/contributing/localizing/"},"formats":{"html":"/zh-hans/4.0/internals/contributing/writing-documentation/","markdown":"/zh-hans/4.0/internals/contributing/writing-documentation.md","json":"/zh-hans/4.0/internals/contributing/writing-documentation.json"},"source":"https://github.com/django/django/blob/stable/4.0.x/docs/internals/contributing/writing-documentation.txt","official":"https://docs.djangoproject.com/zh-hans/4.0/internals/contributing/writing-documentation/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}