{"title":"编写你的第一个 Django 应用程序，第 8 部分","version":"6.1","locale":"zh-hans","docname":"intro/tutorial08","url":"/zh-hans/6.1/intro/tutorial08/","canonical":"https://djangodocs.dev/zh-hans/6.1/intro/tutorial08/","summary":"这个教程从 教程 7 结束的地方开始。我们已经构建了我们的 Web 投票应用程序，现在将看看第三方包。Django 的一个优势就是丰富的第三方包生态系统。这些是由社区开发的包，可以用来快速扩展应用程序的功能集。 本教程将展示如何添加 Django Debug Toolbar ，这是一个常用的第三方包。Django Debug…","html":"<h1>编写你的第一个 Django 应用程序，第 8 部分<a class=\"heading-anchor\" href=\"#writing-your-first-django-app-part-8\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>这个教程从 <a class=\"reference internal\" href=\"/zh-hans/6.1/intro/tutorial07/\"><span class=\"doc\">教程 7</span></a> 结束的地方开始。我们已经构建了我们的 Web 投票应用程序，现在将看看第三方包。Django 的一个优势就是丰富的第三方包生态系统。这些是由社区开发的包，可以用来快速扩展应用程序的功能集。</p>\n<p>本教程将展示如何添加 <a class=\"extlink-pypi reference external\" href=\"https://pypi.org/project/django-debug-toolbar/\">Django Debug Toolbar</a>，这是一个常用的第三方包。Django Debug Toolbar 在近年来的 Django 开发者调查中一直位列最常用的第三方包前三名。</p>\n<aside class=\"admonition-where-to-get-help admonition\">\n<p class=\"admonition-title\">从哪里获得帮助：</p>\n<p>如果你在阅读本教程的过程中有任何疑问，可以前往 FAQ 的 <a class=\"reference internal\" href=\"/zh-hans/6.1/faq/help/\"><span class=\"doc\">获取帮助</span></a> 的版块。</p>\n</aside>\n<section id=\"installing-django-debug-toolbar\">\n<h2>安装 Django Debug Toolbar<a class=\"heading-anchor\" href=\"#installing-django-debug-toolbar\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django Debug Toolbar 是一个用于调试 Django Web 应用程序的有用工具。它是一个由社区组织 <a class=\"reference external\" href=\"https://github.com/django-commons\">Django Commons</a> 维护的第三方包。该工具栏通过提供显示当前请求和响应的调试信息的面板，帮助你理解应用程序的运行方式并识别问题。</p>\n<p>要安装类似工具栏的第三方应用程序，你需要在已激活的虚拟环境中运行以下命令来安装包。这与我们之前安装 Django 的步骤类似：<a class=\"reference internal\" href=\"/zh-hans/6.1/topics/install/#installing-official-release\"><span class=\"std std-ref\">安装官方版本</span></a>。</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>django-debug-toolbar\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 django-debug-toolbar\n</code></pre></div></div></div>\n<p>与 Django 集成的第三方包需要一些安装后的设置，以将它们与你的项目整合在一起。通常，你需要将包的 Django 应用程序添加到你的 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> 设置中。有些包需要其他更改，比如添加到你的 URL 配置（<code class=\"docutils literal notranslate\"><span class=\"pre\">urls.py</span></code>）中。</p>\n<p>Django Debug Toolbar 需要进行几个设置步骤。请按照 <a class=\"reference external\" href=\"https://django-debug-toolbar.readthedocs.io/en/latest/installation.html\">它的安装指南</a> 中的步骤进行操作。这些步骤不会在本教程中重复，因为作为一个第三方包，它可能会单独更改而不按照 Django 的计划进行更新。</p>\n<p>安装完成后，当你浏览到 <code class=\"docutils literal notranslate\"><span class=\"pre\">http://localhost:8000/admin/</span></code> 时，你应该能够在浏览器窗口的右侧看到 DjDT 的“手柄”。点击它以打开调试工具栏，并使用每个面板中的工具。有关面板显示内容的更多信息，请参阅 <a class=\"reference external\" href=\"https://django-debug-toolbar.readthedocs.io/en/latest/panels.html\">面板文档页面</a>。</p>\n</section>\n<section id=\"getting-help-from-others\">\n<h2>寻求他人的帮助<a class=\"heading-anchor\" href=\"#getting-help-from-others\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>在某些情况下，你可能会遇到问题，例如工具栏可能无法渲染。当发生这种情况并且你无法自行解决问题时，有一些可供你选择的选项。</p>\n<ol class=\"arabic simple\">\n<li><p>如果问题出现在某个特定包上，请查阅该包的文档中是否有故障排除指南或常见问题解答。例如 Django Debug Toolbar 就包含一个 <a class=\"reference external\" href=\"https://django-debug-toolbar.readthedocs.io/en/latest/tips.html\">使用技巧章节</a> ，其中列出了相关的故障排除方案。</p></li>\n<li><p>在包的 issue 跟踪器上搜索类似的问题。Django Debug Toolbar 的 issue 跟踪器位于 <a class=\"reference external\" href=\"https://github.com/django-commons/django-debug-toolbar/issues\">GitHub</a>。</p></li>\n<li><p>请参阅 <a class=\"reference external\" href=\"https://forum.djangoproject.com/\">Django 论坛</a>。</p></li>\n<li><p>加入 <a class=\"reference external\" href=\"https://chat.djangoproject.com\">Django Discord server</a>.</p></li>\n</ol>\n</section>\n<section id=\"installing-other-third-party-packages\">\n<h2>安装其他第三方包<a class=\"heading-anchor\" href=\"#installing-other-third-party-packages\"><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://djangopackages.org/\">Django Packages</a> 来查找更多的第三方包。</p>\n<p>很难知道你应该使用哪些第三方包。这取决于你的需求和目标。有时使用处于 alpha 状态的包是可以的，而其他时候，你需要知道它已经准备好用于生产环境。<a class=\"reference external\" href=\"https://adamj.eu/tech/2021/11/04/the-well-maintained-test/\">Adam Johnson 写了一篇博文</a>，概述了一组将包标记为 &quot;维护良好&quot; 的特征。Django Packages 显示了一些这些特征的数据，例如包的最后更新时间。</p>\n<p>正如 Adam 在他的文章中指出的那样，当某个问题的答案是 &quot;否&quot; 时，这就是一个贡献的机会。</p>\n</section>\n<section id=\"what-s-next\">\n<h2>接下来要做什么？<a class=\"heading-anchor\" href=\"#what-s-next\"><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/6.1/intro/whatsnext/\"><span class=\"doc\">下一步看什么</span></a>，看看下一步能做什么。</p>\n<p>如果你很熟悉 Python 打包，且对学习如何把投票应用改成“可复用应用”感兴趣，查看 <a class=\"reference internal\" href=\"/zh-hans/6.1/intro/reusable-apps/\"><span class=\"doc\">进阶教程：如何创建可复用应用</span></a>。</p>\n</section>","rootId":"writing-your-first-django-app-part-8","toc":[{"title":"安装 Django Debug Toolbar","anchor":"installing-django-debug-toolbar","children":[]},{"title":"寻求他人的帮助","anchor":"getting-help-from-others","children":[]},{"title":"安装其他第三方包","anchor":"installing-other-third-party-packages","children":[]},{"title":"接下来要做什么？","anchor":"what-s-next","children":[]}],"breadcrumbs":[{"docname":"intro/index","title":"开始","url":"/zh-hans/6.1/intro/"}],"prev":{"docname":"intro/tutorial07","title":"编写你的第一个 Django 应用，第 7 部分","url":"/zh-hans/6.1/intro/tutorial07/"},"next":{"docname":"intro/reusable-apps","title":"进阶指南：如何编写可重用程序","url":"/zh-hans/6.1/intro/reusable-apps/"},"formats":{"html":"/zh-hans/6.1/intro/tutorial08/","markdown":"/zh-hans/6.1/intro/tutorial08.md","json":"/zh-hans/6.1/intro/tutorial08.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/intro/tutorial08.txt","official":"https://docs.djangoproject.com/zh-hans/6.1/intro/tutorial08/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}