{"title":"如何管理静态文件（如图片、JavaScript、CSS）","version":"5.1","locale":"zh-hans","docname":"howto/static-files/index","url":"/zh-hans/5.1/howto/static-files/","canonical":"https://djangodocs.dev/zh-hans/5.1/howto/static-files/","summary":"网站通常需要提供类似图片，JavaScript 或 CSS 的额外文件服务。在 Django 中，我们将这些文件称为“静态文件”。Django 提供了 django.contrib.staticfiles 帮你管理它们。 本页介绍如何为这些静态文件提供服务。 配置静态文件 Link to this heading # 确保…","html":"<h1>如何管理静态文件（如图片、JavaScript、CSS）<a class=\"heading-anchor\" href=\"#how-to-manage-static-files-e-g-images-javascript-css\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>网站通常需要提供类似图片，JavaScript 或 CSS 的额外文件服务。在 Django 中，我们将这些文件称为“静态文件”。Django 提供了 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code></a> 帮你管理它们。</p>\n<p>本页介绍如何为这些静态文件提供服务。</p>\n<section id=\"configuring-static-files\">\n<h2>配置静态文件<a class=\"heading-anchor\" href=\"#configuring-static-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<ol class=\"arabic\">\n<li><p>确保 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> 包含了 <code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code>。</p></li>\n<li><p>在配置文件中，定义 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_URL</span></code></a>，例子:</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=\"n\">STATIC_URL</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;static/&quot;</span>\n</code></pre></div>\n</li>\n<li><p>在模板中，可以使用 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/templates/builtins/#std-templatetag-static\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">static</span></code></a> 模板标签来使用配置的 <code class=\"docutils literal notranslate\"><span class=\"pre\">staticfiles</span></code> <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STORAGES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STORAGES</span></code></a> 别名构建给定相对路径的 URL。</p>\n<div class=\"code-block\" data-language=\"html+django\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Django template</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=\"Django template code\"><code><span class=\"cp\">{%</span> <span class=\"k\">load</span> <span class=\"nv\">static</span> <span class=\"cp\">%}</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">img</span> <span class=\"na\">src</span><span class=\"o\">=</span><span class=\"s\">&quot;</span><span class=\"cp\">{%</span> <span class=\"k\">static</span> <span class=\"s1\">&#39;my_app/example.jpg&#39;</span> <span class=\"cp\">%}</span><span class=\"s\">&quot;</span> <span class=\"na\">alt</span><span class=\"o\">=</span><span class=\"s\">&quot;My image&quot;</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n</li>\n<li><p>将你的静态文件保存至程序中名为 <code class=\"docutils literal notranslate\"><span class=\"pre\">static</span></code> 的目录中。例如 <code class=\"docutils literal notranslate\"><span class=\"pre\">my_app/static/my_app/example.jpg</span></code>。</p></li>\n</ol>\n<aside class=\"admonition-serving-the-files admonition\">\n<p class=\"admonition-title\">为这些文件提供服务</p>\n<p>除了这些配置步骤外，你还需要实际地为这些文件提供服务。</p>\n<p>开发时，使用 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code></a>，这一般会在 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a> is set to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> 情况下由 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">runserver</span></code></a> 自动完成（参考 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/contrib/staticfiles/#django.contrib.staticfiles.views.serve\" title=\"django.contrib.staticfiles.views.serve\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles.views.serve()</span></code></a>）。</p>\n<p>该方法 <strong>极度低效</strong> 且 <strong>不怎么安全</strong>，所以这 <strong>不适合生产环境</strong>。</p>\n<p>参考 <a class=\"reference internal\" href=\"/zh-hans/5.1/howto/static-files/deployment/\"><span class=\"doc\">如何部署静态文件</span></a> 了解如何正确地在生产环境提供静态文件服务的策略。</p>\n</aside>\n<p>你的工程可能包含未与任何应用绑定的静态资源。除了在 apps 中使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">static/</span></code> 目录，你可以在配置文件中定义一个目录列表 (<a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATICFILES_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATICFILES_DIRS</span></code></a>) ，Django 会从中寻找静态文件。例子:</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=\"n\">STATICFILES_DIRS</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"n\">BASE_DIR</span> <span class=\"o\">/</span> <span class=\"s2\">&quot;static&quot;</span><span class=\"p\">,</span>\n    <span class=\"s2\">&quot;/var/www/static/&quot;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>参考 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATICFILES_FINDERS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATICFILES_FINDERS</span></code></a> 配置的文档了解 <code class=\"docutils literal notranslate\"><span class=\"pre\">staticfiles</span></code> 是如何找到你的文件的细节。</p>\n<aside class=\"admonition-static-file-namespacing admonition\">\n<p class=\"admonition-title\">静态文件命名空间</p>\n<p>现在，我们 <em>也许</em> 能直接将静态文件放入 <code class=\"docutils literal notranslate\"><span class=\"pre\">my_app/static/</span></code> 目录（而不是创建另一个 <code class=\"docutils literal notranslate\"><span class=\"pre\">my_app</span></code> 子目录），不过这实际上是个坏主意。Django 会使用第一个名字匹配的静态文件，若你在 <em>不同的</em> 应用程序中有一个同名静态文件，Django 无力区分它们。我们需要为 Django 指出正确的那个，而最好的方式就是使用 <em>命名空间</em>。也就是说，将这些静态文件放在 <em>另一个</em> 以应用程序名本身命名的目录下。</p>\n<p>你可以通过指定 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#staticfiles-dirs-prefixes\"><span class=\"std std-ref\">prefixes</span></a> 在 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATICFILES_DIRS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATICFILES_DIRS</span></code></a> 中为静态资源做命名。</p>\n</aside>\n</section>\n<section id=\"serving-static-files-during-development\">\n<span id=\"serving-static-files-in-development\"></span><h2>开发时提供静态文件服务<a class=\"heading-anchor\" href=\"#serving-static-files-during-development\"><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/5.1/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code></a>， <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/django-admin/#django-admin-runserver\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">runserver</span></code></a> 会在 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-DEBUG\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DEBUG</span></code></a> 为 <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> 时自动处理。若你未在 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> 中包含 <code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code>，你仍能手动通过 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/views/#django.views.static.serve\" title=\"django.views.static.serve\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.views.static.serve()</span></code></a> 为静态文件提供服务。</p>\n<p>这不适合生产环境！常见的部署策略请参考 <a class=\"reference internal\" href=\"/zh-hans/5.1/howto/static-files/deployment/\"><span class=\"doc\">如何部署静态文件</span></a>。</p>\n<p>例如，如果你的 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_URL</span></code></a> 定义为 <code class=\"docutils literal notranslate\"><span class=\"pre\">static/</span></code>，你可以通过将以下片段添加到你的 <code class=\"docutils literal notranslate\"><span class=\"pre\">urls.py</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=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">settings</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf.urls.static</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">static</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"c1\"># ... the rest of your URLconf goes here ...</span>\n<span class=\"p\">]</span> <span class=\"o\">+</span> <span class=\"n\">static</span><span class=\"p\">(</span><span class=\"n\">settings</span><span class=\"o\">.</span><span class=\"n\">STATIC_URL</span><span class=\"p\">,</span> <span class=\"n\">document_root</span><span class=\"o\">=</span><span class=\"n\">settings</span><span class=\"o\">.</span><span class=\"n\">STATIC_ROOT</span><span class=\"p\">)</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>这个帮助函数只在调试模式下工作，而且只在给定的前缀是本地的（例如 <code class=\"docutils literal notranslate\"><span class=\"pre\">static/</span></code>），而不是一个 URL（例如 <code class=\"docutils literal notranslate\"><span class=\"pre\">http://static.example.com/</span></code> ）时才有效。</p>\n<p>当然，助手函数只为实际的 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a> 目录提供服务；它不会像 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code></a> 一样搜索静态文件。</p>\n<p>最后，静态文件通过 WSGI 应用程序层的包装器提供服务。因此，静态文件请求不会经过正常的 <a class=\"reference internal\" href=\"/zh-hans/5.1/topics/http/middleware/\"><span class=\"doc\">中间件链</span></a>。</p>\n</aside>\n</section>\n<section id=\"serving-files-uploaded-by-a-user-during-development\">\n<span id=\"serving-uploaded-files-in-development\"></span><h2>开发期间保存用户上传的文件<a class=\"heading-anchor\" href=\"#serving-files-uploaded-by-a-user-during-development\"><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/5.1/ref/views/#django.views.static.serve\" title=\"django.views.static.serve\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">django.views.static.serve()</span></code></a> 视图从 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-MEDIA_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_ROOT</span></code></a> 为用户上传的媒体文件提供服务。</p>\n<p>这不适合生产环境！常见的部署策略请参考 <a class=\"reference internal\" href=\"/zh-hans/5.1/howto/static-files/deployment/\"><span class=\"doc\">如何部署静态文件</span></a>。</p>\n<p>例如，如果你的 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code></a> 定义为 <code class=\"docutils literal notranslate\"><span class=\"pre\">media/</span></code>，你可以通过将以下片段添加到你的 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-ROOT_URLCONF\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ROOT_URLCONF</span></code></a> 中来实现：</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=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">settings</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.conf.urls.static</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">static</span>\n\n<span class=\"n\">urlpatterns</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"c1\"># ... the rest of your URLconf goes here ...</span>\n<span class=\"p\">]</span> <span class=\"o\">+</span> <span class=\"n\">static</span><span class=\"p\">(</span><span class=\"n\">settings</span><span class=\"o\">.</span><span class=\"n\">MEDIA_URL</span><span class=\"p\">,</span> <span class=\"n\">document_root</span><span class=\"o\">=</span><span class=\"n\">settings</span><span class=\"o\">.</span><span class=\"n\">MEDIA_ROOT</span><span class=\"p\">)</span>\n</code></pre></div>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>这个帮助函数只在调试模式下工作，而且只在给定的前缀是本地的（例如 <code class=\"docutils literal notranslate\"><span class=\"pre\">media/</span></code>），而不是一个URL（例如 <code class=\"docutils literal notranslate\"><span class=\"pre\">http://media.example.com/</span></code> ）时才有效。</p>\n</aside>\n</section>\n<section id=\"testing\">\n<span id=\"staticfiles-testing-support\"></span><h2>测试<a class=\"heading-anchor\" href=\"#testing\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>运行使用真实 HTTP 请求（而不是内置的测试客户端，即内置的 <a class=\"reference internal\" href=\"/zh-hans/5.1/topics/testing/tools/#django.test.LiveServerTestCase\" title=\"django.test.LiveServerTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">LiveServerTestCase</span></code></a>）的测试用例时，静态资源要与剩余内容分别提供服务，这样，测试环境才能尽量重现真实的问题。但 <code class=\"docutils literal notranslate\"><span class=\"pre\">LiveServerTestCase</span></code> 只拥有非常基本的为静态文件提供服务的能力：它并不知道 <code class=\"docutils literal notranslate\"><span class=\"pre\">staticfiles</span></code> 应用的查找功能，且总是假设静态内容已被收集至 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a> 目录下。</p>\n<p>因此， <code class=\"docutils literal notranslate\"><span class=\"pre\">staticfiles</span></code> 自带了 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/contrib/staticfiles/#django.contrib.staticfiles.testing.StaticLiveServerTestCase\" title=\"django.contrib.staticfiles.testing.StaticLiveServerTestCase\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles.testing.StaticLiveServerTestCase</span></code></a>，这是一个内置子类，能够透明地以类似我们在开发阶段 <code class=\"docutils literal notranslate\"><span class=\"pre\">DEBUG</span> <span class=\"pre\">=</span> <span class=\"pre\">True</span></code> 时获得的方式为所有静态资源在测试期间提供服务。即无需先用 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> 收集它们。</p>\n</section>\n<section id=\"deployment\">\n<h2>部署<a class=\"heading-anchor\" href=\"#deployment\"><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/5.1/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code></a> 提供了一个便利的管理命令，用于将静态文件收集至独立目录，方便你为它们提供服务。</p>\n<ol class=\"arabic\">\n<li><p>将 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a> 配置成你喜欢的目录，在这个目录提供服务，例如:</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=\"n\">STATIC_ROOT</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;/var/www/example.com/static/&quot;</span>\n</code></pre></div>\n</li>\n<li><p>运行 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/contrib/staticfiles/#django-admin-collectstatic\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">collectstatic</span></code></a> 管理命令：</p>\n<div class=\"code-block\" data-language=\"shell\"><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=\"w\"> </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>collectstatic\n</code></pre></div>\n<p>这将会把静态目录下的所有文件拷贝至 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a> 目录。</p>\n</li>\n<li><p>选一个 Web 服务器为这些文件提供服务。 文档 <a class=\"reference internal\" href=\"/zh-hans/5.1/howto/static-files/deployment/\"><span class=\"doc\">如何部署静态文件</span></a> 介绍了静态文件的常见部署策略。</p></li>\n</ol>\n</section>\n<section id=\"learn-more\">\n<h2>了解更多<a class=\"heading-anchor\" href=\"#learn-more\"><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/5.1/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code></a> 碎片的全部细节，参考 <a class=\"reference internal\" href=\"/zh-hans/5.1/ref/contrib/staticfiles/\"><span class=\"doc\">静态文件参考</span></a>。</p>\n</section>","rootId":"how-to-manage-static-files-e-g-images-javascript-css","toc":[{"title":"配置静态文件","anchor":"configuring-static-files","children":[]},{"title":"开发时提供静态文件服务","anchor":"serving-static-files-during-development","children":[]},{"title":"开发期间保存用户上传的文件","anchor":"serving-files-uploaded-by-a-user-during-development","children":[]},{"title":"测试","anchor":"testing","children":[]},{"title":"部署","anchor":"deployment","children":[]},{"title":"了解更多","anchor":"learn-more","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"如何引导","url":"/zh-hans/5.1/howto/"}],"prev":{"docname":"howto/custom-template-tags","title":"如何编写自定义的模板标签和过滤器","url":"/zh-hans/5.1/howto/custom-template-tags/"},"next":{"docname":"howto/logging","title":"如何配置和使用日志","url":"/zh-hans/5.1/howto/logging/"},"formats":{"html":"/zh-hans/5.1/howto/static-files/","markdown":"/zh-hans/5.1/howto/static-files.md","json":"/zh-hans/5.1/howto/static-files.json"},"source":"https://github.com/django/django/blob/stable/5.1.x/docs/howto/static-files/index.txt","official":"https://docs.djangoproject.com/zh-hans/5.1/howto/static-files/","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"]}