{"title":"表单资源（ Media 类）","version":"6.1","locale":"zh-hans","docname":"topics/forms/media","url":"/zh-hans/6.1/topics/forms/media/","canonical":"https://djangodocs.dev/zh-hans/6.1/topics/forms/media/","summary":"呈现一个有吸引力的、易于使用的网络表格需要的不仅仅是 HTML，它还需要 CSS 样式表，如果你想使用花哨的部件，你可能还需要在每个页面上包含一些 JavaScript。","html":"<h1>表单资源（ <code class=\"docutils literal notranslate\">Media</code> 类）<a class=\"heading-anchor\" href=\"#form-assets-the-media-class\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>呈现一个有吸引力的、易于使用的网络表格需要的不仅仅是 HTML，它还需要 CSS 样式表，如果你想使用花哨的部件，你可能还需要在每个页面上包含一些 JavaScript。任何特定页面所需的 CSS 和 JavaScript 的确切组合将取决于该页面上使用的部件。</p>\n<p>这就是资源定义的作用。Django 允许你将不同的文件——如样式表和脚本——与需要这些资产的表单和部件联系起来。例如，如果你想用一个日历来渲染 DateFields，你可以定义一个自定义的日历部件。然后这个部件可以与渲染日历所需的 CSS 和 JavaScript 相关联。当日历部件在表单上使用时，Django 能够识别需要的 CSS 和 JavaScript 文件，并以适合包含在你的网页上的形式提供文件名列表。</p>\n<aside class=\"admonition-assets-and-django-admin admonition\" role=\"note\">\n<p class=\"admonition-title\">资源及Django Admin</p>\n<p>Django Admin应用程序为日历、选择过滤及其他功能定义了一些定制的组件。这些组件定义资源的需求，Django Admin使用自定义组件来代替Django的默认组件。Admin模板将只会包含在页面上呈现组件所需的文件。</p>\n<p>如果您喜欢Django Admin应用程序使用的组件，您可以在应用中随意使用它们。它们都位于 <code class=\"docutils literal notranslate\">django.contrib.admin.widgets</code> 。</p>\n</aside>\n<aside class=\"admonition-which-javascript-toolkit admonition\" role=\"note\">\n<p class=\"admonition-title\">哪个JavaScript工具包？</p>\n<p>现在有很多JavaScript工具包，它们中许多都包含组件（比如日历组件），可以用来改善您的应用程序。Django刻意避免去推荐任何一个JavaScript工具包。每个工具包都有自己的优点和缺点，使用适合您需求的工具包。Django能够与任何JavaScript工具包集成。</p>\n</aside>\n<section id=\"assets-as-a-static-definition\">\n<span id=\"id1\"></span><h2>资源作为静态定义<a class=\"heading-anchor\" href=\"#assets-as-a-static-definition\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>定义资源最简单方法是静态定义。要使用这种方法，声明是一个内部的 <code class=\"docutils literal notranslate\">Media</code> 类。此内部类的属性定义了这个需求。</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><span class=\"kn\">from</span> <span class=\"nn\">django</span> <span class=\"kn\">import</span> forms\n\n\n<span class=\"k\">class</span> <span class=\"nc\">CalendarWidget</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>TextInput<span class=\"p\">):</span>\n    <span class=\"k\">class</span> <span class=\"nc\">Media</span><span class=\"p\">:</span>\n        css <span class=\"o\">=</span> <span class=\"p\">{</span>\n            <span class=\"s2\">&quot;all&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;pretty.css&quot;</span><span class=\"p\">],</span>\n        <span class=\"p\">}</span>\n        js <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;animations.js&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;actions.js&quot;</span><span class=\"p\">]</span>\n</code></pre></div>\n<p>这段代码定义了一个 <code class=\"docutils literal notranslate\">CalendarWidget</code> ，它继承自 <code class=\"docutils literal notranslate\">TextInput</code> 。每次CalendarWidget在表单上使用时，该表单都会包含CSS文件 <code class=\"docutils literal notranslate\">pretty.css</code> ，以及JavaScript文件 <code class=\"docutils literal notranslate\">animations.js</code> 和 <code class=\"docutils literal notranslate\">actions.js</code> 。</p>\n<p>这个静态定义在运行时被转换为一个名为 <code class=\"docutils literal notranslate\">media</code> 的小部件属性。可以通过这个属性获取 <code class=\"docutils literal notranslate\">CalendarWidget</code> 实例的资产列表：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>w <span class=\"o\">=</span> CalendarWidget<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span>w<span class=\"o\">.</span>media<span class=\"p\">)</span>\n<span class=\"go\">&lt;link href=&quot;https://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>以下是所有可能的 <code class=\"docutils literal notranslate\">Media</code> 选项列表。没有一个是必需项。</p>\n<section id=\"css\">\n<h3><code class=\"docutils literal notranslate\">css</code><a class=\"heading-anchor\" href=\"#css\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>描述各种表单输出媒体所需的CSS文件的字典。</p>\n<p>字典中的值应该是一个文件名元组/列表。有关如何指定这些文件的路径的详细内容，请参阅 <a class=\"reference internal\" href=\"#form-asset-paths\"><span class=\"std std-ref\">路径章节</span></a> 。</p>\n<p>字典中的键是输出媒体类型。它们和媒体声明中CSS文件接受的类型相同：'all'、'aural'、'braille'、'embossed'、'handheld'、'print'、'projection'、'screen'、'tty' 和 'tv'。如果您需要针对不同媒体类型使用不同的样式表，就要给每个输出媒体提供一个CSS文件列表。下面的示例提供了两个CSS选项——一个用于屏幕，一个用于打印：</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=\"k\">class</span> <span class=\"nc\">Media</span><span class=\"p\">:</span>\n    css <span class=\"o\">=</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;screen&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;pretty.css&quot;</span><span class=\"p\">],</span>\n        <span class=\"s2\">&quot;print&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;newspaper.css&quot;</span><span class=\"p\">],</span>\n    <span class=\"p\">}</span>\n</code></pre></div>\n<p>如果一组CSS文件适用于多种输出媒体类型，字典的键可以是以逗号分隔的输出媒体类型列表。在下面的例子中，电视和投影机将具有相同的媒体需求：</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=\"k\">class</span> <span class=\"nc\">Media</span><span class=\"p\">:</span>\n    css <span class=\"o\">=</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;screen&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;pretty.css&quot;</span><span class=\"p\">],</span>\n        <span class=\"s2\">&quot;tv,projector&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;lo_res.css&quot;</span><span class=\"p\">],</span>\n        <span class=\"s2\">&quot;print&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;newspaper.css&quot;</span><span class=\"p\">],</span>\n    <span class=\"p\">}</span>\n</code></pre></div>\n<p>如果这个最后的 CSS 定义要被渲染，它将变成以下 HTML：</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=\"p\">&lt;</span><span class=\"nt\">link</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;https://static.example.com/pretty.css&quot;</span> <span class=\"na\">media</span><span class=\"o\">=</span><span class=\"s\">&quot;screen&quot;</span> <span class=\"na\">rel</span><span class=\"o\">=</span><span class=\"s\">&quot;stylesheet&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">link</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;https://static.example.com/lo_res.css&quot;</span> <span class=\"na\">media</span><span class=\"o\">=</span><span class=\"s\">&quot;tv,projector&quot;</span> <span class=\"na\">rel</span><span class=\"o\">=</span><span class=\"s\">&quot;stylesheet&quot;</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;</span><span class=\"nt\">link</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;https://static.example.com/newspaper.css&quot;</span> <span class=\"na\">media</span><span class=\"o\">=</span><span class=\"s\">&quot;print&quot;</span> <span class=\"na\">rel</span><span class=\"o\">=</span><span class=\"s\">&quot;stylesheet&quot;</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<section id=\"stylesheet-objects\">\n<h4><code class=\"docutils literal notranslate\">Stylesheet</code> objects<a class=\"heading-anchor\" href=\"#stylesheet-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<aside class=\"version-note version-added\" data-version=\"6.1\" role=\"note\">\n<p class=\"version-note-title\">New in Django 6.1</p></aside>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Stylesheet\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">Stylesheet</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">href</em>, <em class=\"sig-param\"><span class=\"o\">**</span>attributes</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Stylesheet\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents a stylesheet link. The <code class=\"docutils literal notranslate\">rel</code> attribute is set to\n<code class=\"docutils literal notranslate\">&quot;stylesheet&quot;</code>.</p>\n<p>The first parameter, <code class=\"docutils literal notranslate\">href</code>, is the string path to the stylesheet file.\nSee <a class=\"reference internal\" href=\"#form-asset-paths\"><span class=\"std std-ref\">the section on paths</span></a> for details on how to\nspecify paths to these files.</p>\n<p>The optional keyword arguments, <code class=\"docutils literal notranslate\">**attributes</code>, are additional HTML\nattributes set on the rendered <code class=\"docutils literal notranslate\">&lt;link&gt;</code> tag.</p>\n<p>See <a class=\"reference internal\" href=\"#form-media-asset-objects\"><span class=\"std std-ref\">路径作为对象</span></a> for usage examples.</p>\n</dd></dl>\n\n</section>\n</section>\n<section id=\"js\">\n<h3><code class=\"docutils literal notranslate\">js</code><a class=\"heading-anchor\" href=\"#js\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>描述所需JavaScript文件的一个元组。有关如何指定这些文件的路径的详细内容，请参阅 <a class=\"reference internal\" href=\"#form-asset-paths\"><span class=\"std std-ref\">路径章节</span></a> 。</p>\n<section id=\"script-objects\">\n<h4><code class=\"docutils literal notranslate\">Script</code> objects<a class=\"heading-anchor\" href=\"#script-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h4>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Script\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">Script</span><span class=\"sig-paren\">(</span><em class=\"sig-param\">src</em>, <em class=\"sig-param\"><span class=\"o\">**</span>attributes</em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.forms.Script\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents a script file.</p>\n<p>The first parameter, <code class=\"docutils literal notranslate\">src</code>, is the string path to the script file. See\n<a class=\"reference internal\" href=\"#form-asset-paths\"><span class=\"std std-ref\">the section on paths</span></a> for details on how to\nspecify paths to these files.</p>\n<p>The optional keyword arguments, <code class=\"docutils literal notranslate\">**attributes</code>, are HTML attributes that\nare set on the rendered <code class=\"docutils literal notranslate\">&lt;script&gt;</code> tag.</p>\n<p>See <a class=\"reference internal\" href=\"#form-media-asset-objects\"><span class=\"std std-ref\">路径作为对象</span></a> for usage examples.</p>\n</dd></dl>\n\n</section>\n</section>\n<section id=\"extend\">\n<h3><code class=\"docutils literal notranslate\">extend</code><a class=\"heading-anchor\" href=\"#extend\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>定义了 <code class=\"docutils literal notranslate\">Media</code> 声明继承行为的一个布尔值。</p>\n<p>默认情况下，使用静态 <code class=\"docutils literal notranslate\">Media</code> 定义的任何对象都会继承与父小部件关联的所有资产。这将发生无论父级如何定义自己的要求。例如，如果我们要扩展上面示例中的基本日历小部件：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">FancyCalendarWidget</span><span class=\"p\">(</span>CalendarWidget<span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span> <span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        css <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>            <span class=\"s2\">&quot;all&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;fancy.css&quot;</span><span class=\"p\">],</span>\n<span class=\"gp\">... </span>        <span class=\"p\">}</span>\n<span class=\"gp\">... </span>        js <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;whizbang.js&quot;</span><span class=\"p\">]</span>\n<span class=\"gp\">...</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span>w <span class=\"o\">=</span> FancyCalendarWidget<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span>w<span class=\"o\">.</span>media<span class=\"p\">)</span>\n<span class=\"go\">&lt;link href=&quot;https://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;link href=&quot;https://static.example.com/fancy.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/whizbang.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>FancyCalendar 小部件继承了其父级小部件的所有资产。如果你不希望 <code class=\"docutils literal notranslate\">Media</code> 以这种方式被继承，可以在 <code class=\"docutils literal notranslate\">Media</code> 声明中添加一个 <code class=\"docutils literal notranslate\">extend=False</code> 声明：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">FancyCalendarWidget</span><span class=\"p\">(</span>CalendarWidget<span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span> <span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        extend <span class=\"o\">=</span> <span class=\"kc\">False</span>\n<span class=\"gp\">... </span>        css <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>            <span class=\"s2\">&quot;all&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;fancy.css&quot;</span><span class=\"p\">],</span>\n<span class=\"gp\">... </span>        <span class=\"p\">}</span>\n<span class=\"gp\">... </span>        js <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;whizbang.js&quot;</span><span class=\"p\">]</span>\n<span class=\"gp\">...</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span>w <span class=\"o\">=</span> FancyCalendarWidget<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span>w<span class=\"o\">.</span>media<span class=\"p\">)</span>\n<span class=\"go\">&lt;link href=&quot;https://static.example.com/fancy.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/whizbang.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>如果您需要更多的继承控制，用一个 <a class=\"reference internal\" href=\"#dynamic-property\"><span class=\"std std-ref\">动态属性</span></a> 定义你的 。动态属性使您可以完全控制哪些文件是否继承。</p>\n</section>\n</section>\n<section id=\"media-as-a-dynamic-property\">\n<span id=\"dynamic-property\"></span><h2>把 <code class=\"docutils literal notranslate\">Media</code> 作为动态属性<a class=\"heading-anchor\" href=\"#media-as-a-dynamic-property\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If you need to perform some more sophisticated manipulation of asset\nrequirements, you can define the <code class=\"docutils literal notranslate\">media</code> property directly. This is\ndone by defining a widget property that returns an instance of\n<code class=\"docutils literal notranslate\">forms.Media</code>. The constructor for <code class=\"docutils literal notranslate\">forms.Media</code> accepts <code class=\"docutils literal notranslate\">css</code>\nand <code class=\"docutils literal notranslate\">js</code> keyword arguments in the same format as that used in a\nstatic media definition.</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><span class=\"k\">class</span> <span class=\"nc\">CalendarWidget</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>TextInput<span class=\"p\">):</span>\n    <span class=\"nd\">@property</span>\n    <span class=\"k\">def</span> <span class=\"nf\">media</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> forms<span class=\"o\">.</span>Media<span class=\"p\">(</span>\n            css<span class=\"o\">=</span><span class=\"p\">{</span><span class=\"s2\">&quot;all&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;pretty.css&quot;</span><span class=\"p\">]},</span> js<span class=\"o\">=</span><span class=\"p\">[</span><span class=\"s2\">&quot;animations.js&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;actions.js&quot;</span><span class=\"p\">]</span>\n        <span class=\"p\">)</span>\n</code></pre></div>\n<p>更多有关如何为动态 <code class=\"docutils literal notranslate\">media</code> 属性构建返回值的内容，请参阅 <a class=\"reference internal\" href=\"#media-objects\">媒体对象</a> 章节。</p>\n</section>\n<section id=\"paths-in-asset-definitions\">\n<span id=\"form-asset-paths\"></span><h2>资源定义中的路径<a class=\"heading-anchor\" href=\"#paths-in-asset-definitions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"paths-as-strings\">\n<h3>路径作为字符串<a class=\"heading-anchor\" href=\"#paths-as-strings\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>用于指定资产的字符串路径可以是相对路径或绝对路径。如果路径以 <code class=\"docutils literal notranslate\">/</code>、<code class=\"docutils literal notranslate\">http://</code> 或 <code class=\"docutils literal notranslate\">https://</code> 开头，它将被解释为绝对路径，并保持不变。所有其他路径都将以适当前缀的值作为前缀。如果安装了 <a class=\"reference internal\" href=\"/zh-hans/6.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\">django.contrib.staticfiles</code></a> 应用程序，它将用于提供资产。</p>\n<p>无论您是否使用 <a class=\"reference internal\" href=\"/zh-hans/6.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\">django.contrib.staticfiles</code></a> ，都需要设置 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_URL</code></a> 和 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_ROOT</code></a> 来渲染一张完整的网页。</p>\n<p>为了找到要使用的前缀，Django 会检查 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_URL</code></a> 设置是否不为 <code class=\"docutils literal notranslate\">None</code>，并自动回退到使用 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_URL</code></a>。例如，如果你的站点的 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\">MEDIA_URL</code></a> 是 <code class=\"docutils literal notranslate\">'https://uploads.example.com/'</code> 而 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_URL</code></a> 是 <code class=\"docutils literal notranslate\">None</code>：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django</span> <span class=\"kn\">import</span> forms\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">CalendarWidget</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>TextInput<span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span> <span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        css <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>            <span class=\"s2\">&quot;all&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;/css/pretty.css&quot;</span><span class=\"p\">],</span>\n<span class=\"gp\">... </span>        <span class=\"p\">}</span>\n<span class=\"gp\">... </span>        js <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;animations.js&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;https://othersite.com/actions.js&quot;</span><span class=\"p\">]</span>\n<span class=\"gp\">...</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span>w <span class=\"o\">=</span> CalendarWidget<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span>w<span class=\"o\">.</span>media<span class=\"p\">)</span>\n<span class=\"go\">&lt;link href=&quot;/css/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://uploads.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://othersite.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>但如果 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\">STATIC_URL</code></a> 是 <code class=\"docutils literal notranslate\">'https://static.example.com/'</code>：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>w <span class=\"o\">=</span> CalendarWidget<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span>w<span class=\"o\">.</span>media<span class=\"p\">)</span>\n<span class=\"go\">&lt;link href=&quot;/css/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://othersite.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>或者如果使用 <a class=\"reference internal\" href=\"/zh-hans/6.1/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage\" title=\"django.contrib.staticfiles.storage.ManifestStaticFilesStorage\"><code class=\"xref py py-class docutils literal notranslate\">ManifestStaticFilesStorage</code></a> 配置了 <a class=\"reference internal\" href=\"/zh-hans/6.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\">staticfiles</code></a>：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>w <span class=\"o\">=</span> CalendarWidget<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span>w<span class=\"o\">.</span>media<span class=\"p\">)</span>\n<span class=\"go\">&lt;link href=&quot;/css/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/animations.27e20196a850.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://othersite.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"paths-as-objects\">\n<span id=\"form-media-asset-objects\"></span><h3>路径作为对象<a class=\"heading-anchor\" href=\"#paths-as-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Assets may also be object-based, using <a class=\"reference internal\" href=\"#django.forms.Script\" title=\"django.forms.Script\"><code class=\"xref py py-class docutils literal notranslate\">Script</code></a> or\n<a class=\"reference internal\" href=\"#django.forms.Stylesheet\" title=\"django.forms.Stylesheet\"><code class=\"xref py py-class docutils literal notranslate\">Stylesheet</code></a>. These allow you to pass custom HTML attributes:</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=\"k\">class</span> <span class=\"nc\">Media</span><span class=\"p\">:</span>\n    js <span class=\"o\">=</span> <span class=\"p\">[</span>\n        Script<span class=\"p\">(</span>\n            <span class=\"s2\">&quot;https://cdn.example.com/something.min.js&quot;</span><span class=\"p\">,</span>\n            <span class=\"o\">**</span><span class=\"p\">{</span>\n                <span class=\"s2\">&quot;crossorigin&quot;</span><span class=\"p\">:</span> <span class=\"s2\">&quot;anonymous&quot;</span><span class=\"p\">,</span>\n                <span class=\"s2\">&quot;async&quot;</span><span class=\"p\">:</span> <span class=\"kc\">True</span><span class=\"p\">,</span>\n            <span class=\"p\">},</span>\n        <span class=\"p\">),</span>\n    <span class=\"p\">]</span>\n</code></pre></div>\n<p>If this Media definition were to be rendered, it would become the following\nHTML:</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=\"p\">&lt;</span><span class=\"nt\">script</span> <span class=\"na\">src</span><span class=\"o\">=</span><span class=\"s\">&quot;https://cdn.example.com/something.min.js&quot;</span>\n        <span class=\"na\">crossorigin</span><span class=\"o\">=</span><span class=\"s\">&quot;anonymous&quot;</span>\n        <span class=\"na\">async</span><span class=\"p\">&gt;</span>\n<span class=\"p\">&lt;/</span><span class=\"nt\">script</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n<p>Similarly, a <a class=\"reference internal\" href=\"#django.forms.Stylesheet\" title=\"django.forms.Stylesheet\"><code class=\"xref py py-class docutils literal notranslate\">Stylesheet</code></a> object can be used to add a stylesheet\nlink with custom HTML attributes:</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=\"k\">class</span> <span class=\"nc\">Media</span><span class=\"p\">:</span>\n    css <span class=\"o\">=</span> <span class=\"p\">{</span>\n        <span class=\"s2\">&quot;all&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span>\n            Stylesheet<span class=\"p\">(</span>\n                <span class=\"s2\">&quot;https://cdn.example.com/print.css&quot;</span><span class=\"p\">,</span>\n                crossorigin<span class=\"o\">=</span><span class=\"s2\">&quot;anonymous&quot;</span><span class=\"p\">,</span>\n                media<span class=\"o\">=</span><span class=\"s2\">&quot;print&quot;</span><span class=\"p\">,</span>\n            <span class=\"p\">),</span>\n        <span class=\"p\">]</span>\n    <span class=\"p\">}</span>\n</code></pre></div>\n<p>If this Media definition were to be rendered, it would become:</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=\"p\">&lt;</span><span class=\"nt\">link</span> <span class=\"na\">href</span><span class=\"o\">=</span><span class=\"s\">&quot;https://cdn.example.com/print.css&quot;</span>\n      <span class=\"na\">crossorigin</span><span class=\"o\">=</span><span class=\"s\">&quot;anonymous&quot;</span>\n      <span class=\"na\">media</span><span class=\"o\">=</span><span class=\"s\">&quot;print&quot;</span>\n      <span class=\"na\">rel</span><span class=\"o\">=</span><span class=\"s\">&quot;stylesheet&quot;</span><span class=\"p\">&gt;</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"media-objects\">\n<h2><code class=\"docutils literal notranslate\">Media</code> 对象<a class=\"heading-anchor\" href=\"#media-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<dl class=\"py class\">\n<dt class=\"sig sig-object py\" id=\"django.forms.Media\">\n<em class=\"property\"><span class=\"k\">class</span> </em><span class=\"sig-name descname\">Media</span><a class=\"heading-anchor\" href=\"#django.forms.Media\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Represents a collection of media assets required by a widget or form.</p>\n</dd></dl>\n\n<p>当您访问表单或者组件的 <code class=\"docutils literal notranslate\">media</code> 属性时，返回值是一个 <code class=\"docutils literal notranslate\">forms.Media</code> 对象。正如我们已经看到的， <code class=\"docutils literal notranslate\">Media</code> 对象的字符串表示是一段需要在您HTML页面的 <code class=\"docutils literal notranslate\">&lt;head&gt;</code> 块中包含相关文件的HTML代码。</p>\n<p>然而， <code class=\"docutils literal notranslate\">Media</code> 对象还有其他一些有趣的属性。</p>\n<section id=\"subsets-of-assets\">\n<h3>资源的子集<a class=\"heading-anchor\" href=\"#subsets-of-assets\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>如果你只想要特定类型的文件，你可以使用下标运算符来过滤出感兴趣的媒体。例如：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span>w <span class=\"o\">=</span> CalendarWidget<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span>w<span class=\"o\">.</span>media<span class=\"p\">)</span>\n<span class=\"go\">&lt;link href=&quot;https://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span>w<span class=\"o\">.</span>media<span class=\"p\">[</span><span class=\"s2\">&quot;css&quot;</span><span class=\"p\">])</span>\n<span class=\"go\">&lt;link href=&quot;https://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n</code></pre></div>\n<p>当您使用下标运算符时，返回值是一个新的 <code class=\"docutils literal notranslate\">Media</code> 对象——但只包含感兴趣的媒体。</p>\n</section>\n<section id=\"combining-media-objects\">\n<h3>合并 <code class=\"docutils literal notranslate\">Media</code> 对象<a class=\"heading-anchor\" href=\"#combining-media-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p><code class=\"docutils literal notranslate\">Media</code> 对象也可以相互相加。当两个 <code class=\"docutils literal notranslate\">Media</code> 对象相加时，结果的 <code class=\"docutils literal notranslate\">Media</code> 对象包含两者指定的资产的并集：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django</span> <span class=\"kn\">import</span> forms\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">CalendarWidget</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>TextInput<span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span> <span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        css <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>            <span class=\"s2\">&quot;all&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;pretty.css&quot;</span><span class=\"p\">],</span>\n<span class=\"gp\">... </span>        <span class=\"p\">}</span>\n<span class=\"gp\">... </span>        js <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;animations.js&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;actions.js&quot;</span><span class=\"p\">]</span>\n<span class=\"gp\">...</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">OtherWidget</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>TextInput<span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span> <span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        js <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;whizbang.js&quot;</span><span class=\"p\">]</span>\n<span class=\"gp\">...</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span>w1 <span class=\"o\">=</span> CalendarWidget<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>w2 <span class=\"o\">=</span> OtherWidget<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span>w1<span class=\"o\">.</span>media <span class=\"o\">+</span> w2<span class=\"o\">.</span>media<span class=\"p\">)</span>\n<span class=\"go\">&lt;link href=&quot;https://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/whizbang.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"order-of-assets\">\n<span id=\"form-media-asset-order\"></span><h3>资源的排序<a class=\"heading-anchor\" href=\"#order-of-assets\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>资源插入DOM的顺序一般来说很重要。例如，您可能有一个依赖于jQuery的脚本。因此，合并 <code class=\"docutils literal notranslate\">Media</code> 对象会尝试保持资源在每个 <code class=\"docutils literal notranslate\">Media</code> 类中定义的相对顺序。</p>\n<p>例如：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django</span> <span class=\"kn\">import</span> forms\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">CalendarWidget</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>TextInput<span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span> <span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        js <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;jQuery.js&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;calendar.js&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;noConflict.js&quot;</span><span class=\"p\">]</span>\n<span class=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">TimeWidget</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>TextInput<span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span> <span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        js <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;jQuery.js&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;time.js&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;noConflict.js&quot;</span><span class=\"p\">]</span>\n<span class=\"gp\">...</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>w1 <span class=\"o\">=</span> CalendarWidget<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>w2 <span class=\"o\">=</span> TimeWidget<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span>w1<span class=\"o\">.</span>media <span class=\"o\">+</span> w2<span class=\"o\">.</span>media<span class=\"p\">)</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/jQuery.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/calendar.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/time.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/noConflict.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>合并 <code class=\"docutils literal notranslate\">Media</code> 对象时，如果资源排序冲突，会导致警告提示： <code class=\"docutils literal notranslate\">MediaOrderConflictWarning</code> 。</p>\n</section>\n</section>\n<section id=\"media-on-forms\">\n<h2>表单上的 <code class=\"docutils literal notranslate\">Media</code><a class=\"heading-anchor\" href=\"#media-on-forms\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>组件不是唯一可以具有 <code class=\"docutils literal notranslate\">media</code> 定义的对象——表单也可以。表单上 <code class=\"docutils literal notranslate\">media</code> 定义的规则与组件的规则相同：声明可以是静态的或动态的；声明的路径和继承规则也一模一样。</p>\n<p>不管你是否定义了一个 <code class=\"docutils literal notranslate\">media</code> 声明，<em>所有</em> Form 对象都有一个 <code class=\"docutils literal notranslate\">media</code> 属性。该属性的默认值是将所有组成表单的小部件的 <code class=\"docutils literal notranslate\">media</code> 定义相加的结果：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span> <span class=\"nn\">django</span> <span class=\"kn\">import</span> forms\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">ContactForm</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>Form<span class=\"p\">):</span>\n<span class=\"gp\">... </span>    date <span class=\"o\">=</span> DateField<span class=\"p\">(</span>widget<span class=\"o\">=</span>CalendarWidget<span class=\"p\">)</span>\n<span class=\"gp\">... </span>    name <span class=\"o\">=</span> CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">40</span><span class=\"p\">,</span> widget<span class=\"o\">=</span>OtherWidget<span class=\"p\">)</span>\n<span class=\"gp\">...</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span>f <span class=\"o\">=</span> ContactForm<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>f<span class=\"o\">.</span>media\n<span class=\"go\">&lt;link href=&quot;https://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/whizbang.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>如果你想将额外的资产与一个表单关联起来，例如，表单布局的 CSS，请向表单添加一个 <code class=\"docutils literal notranslate\">Media</code> 声明：</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span> <span class=\"nc\">ContactForm</span><span class=\"p\">(</span>forms<span class=\"o\">.</span>Form<span class=\"p\">):</span>\n<span class=\"gp\">... </span>    date <span class=\"o\">=</span> DateField<span class=\"p\">(</span>widget<span class=\"o\">=</span>CalendarWidget<span class=\"p\">)</span>\n<span class=\"gp\">... </span>    name <span class=\"o\">=</span> CharField<span class=\"p\">(</span>max_length<span class=\"o\">=</span><span class=\"mi\">40</span><span class=\"p\">,</span> widget<span class=\"o\">=</span>OtherWidget<span class=\"p\">)</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span> <span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        css <span class=\"o\">=</span> <span class=\"p\">{</span>\n<span class=\"gp\">... </span>            <span class=\"s2\">&quot;all&quot;</span><span class=\"p\">:</span> <span class=\"p\">[</span><span class=\"s2\">&quot;layout.css&quot;</span><span class=\"p\">],</span>\n<span class=\"gp\">... </span>        <span class=\"p\">}</span>\n<span class=\"gp\">...</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span>f <span class=\"o\">=</span> ContactForm<span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span>f<span class=\"o\">.</span>media\n<span class=\"go\">&lt;link href=&quot;https://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;link href=&quot;https://static.example.com/layout.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;https://static.example.com/whizbang.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n</section>","rootId":"form-assets-the-media-class","toc":[{"title":"资源作为静态定义","anchor":"assets-as-a-static-definition","children":[{"title":"css","anchor":"css","children":[{"title":"Stylesheet objects","anchor":"stylesheet-objects","children":[]}]},{"title":"js","anchor":"js","children":[{"title":"Script objects","anchor":"script-objects","children":[]}]},{"title":"extend","anchor":"extend","children":[]}]},{"title":"把 Media 作为动态属性","anchor":"media-as-a-dynamic-property","children":[]},{"title":"资源定义中的路径","anchor":"paths-in-asset-definitions","children":[{"title":"路径作为字符串","anchor":"paths-as-strings","children":[]},{"title":"路径作为对象","anchor":"paths-as-objects","children":[]}]},{"title":"Media 对象","anchor":"media-objects","children":[{"title":"资源的子集","anchor":"subsets-of-assets","children":[]},{"title":"合并 Media 对象","anchor":"combining-media-objects","children":[]},{"title":"资源的排序","anchor":"order-of-assets","children":[]}]},{"title":"表单上的 Media","anchor":"media-on-forms","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"使用 Django","url":"/zh-hans/6.1/topics/"},{"docname":"topics/forms/index","title":"使用表单","url":"/zh-hans/6.1/topics/forms/"}],"prev":{"docname":"topics/forms/modelforms","title":"从模型创建表单","url":"/zh-hans/6.1/topics/forms/modelforms/"},"next":{"docname":"topics/templates","title":"模板","url":"/zh-hans/6.1/topics/templates/"},"formats":{"html":"/zh-hans/6.1/topics/forms/media/","markdown":"/zh-hans/6.1/topics/forms/media.md","json":"/zh-hans/6.1/topics/forms/media.json"},"source":"https://github.com/django/django/blob/stable/6.1.x/docs/topics/forms/media.txt","official":"https://docs.djangoproject.com/zh-hans/6.1/topics/forms/media/","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","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}