{"title":"Form Assets (the Media class)","version":"5.0","locale":"es","docname":"topics/forms/media","url":"/es/5.0/topics/forms/media/","canonical":"https://djangodocs.dev/es/5.0/topics/forms/media/","summary":"Rendering an attractive and easy-to-use web form requires more than just HTML - it also requires CSS stylesheets, and if you want to use fancy widgets, you may also…","html":"<h1>Form Assets (the <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> class)<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>Rendering an attractive and easy-to-use web form requires more than just\nHTML - it also requires CSS stylesheets, and if you want to use fancy widgets,\nyou may also need to include some JavaScript on each page. The exact\ncombination of CSS and JavaScript that is required for any given page will\ndepend upon the widgets that are in use on that page.</p>\n<p>This is where asset definitions come in. Django allows you to\nassociate different files – like stylesheets and scripts – with the\nforms and widgets that require those assets. For example, if you want\nto use a calendar to render DateFields, you can define a custom\nCalendar widget. This widget can then be associated with the CSS and\nJavaScript that is required to render the calendar. When the Calendar\nwidget is used on a form, Django is able to identify the CSS and\nJavaScript files that are required, and provide the list of file names\nin a form suitable for inclusion on your web page.</p>\n<aside class=\"admonition-assets-and-django-admin admonition\">\n<p class=\"admonition-title\">Assets and Django Admin</p>\n<p>The Django Admin application defines a number of customized\nwidgets for calendars, filtered selections, and so on. These\nwidgets define asset requirements, and the Django Admin uses the\ncustom widgets in place of the Django defaults. The Admin\ntemplates will only include those files that are required to\nrender the widgets on any given page.</p>\n<p>If you like the widgets that the Django Admin application uses,\nfeel free to use them in your own application! They’re all stored\nin <code class=\"docutils literal notranslate\"><span class=\"pre\">django.contrib.admin.widgets</span></code>.</p>\n</aside>\n<aside class=\"admonition-which-javascript-toolkit admonition\">\n<p class=\"admonition-title\">Which JavaScript toolkit?</p>\n<p>Many JavaScript toolkits exist, and many of them include widgets (such\nas calendar widgets) that can be used to enhance your application.\nDjango has deliberately avoided blessing any one JavaScript toolkit.\nEach toolkit has its own relative strengths and weaknesses - use\nwhichever toolkit suits your requirements. Django is able to integrate\nwith any JavaScript toolkit.</p>\n</aside>\n<section id=\"assets-as-a-static-definition\">\n<span id=\"id1\"></span><h2>Assets as a static definition<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>The easiest way to define assets is as a static definition. Using this\nmethod, the declaration is an inner <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> class. The properties of the\ninner class define the requirements.</p>\n<p>Here’s an example:</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</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">forms</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">CalendarWidget</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">TextInput</span><span class=\"p\">):</span>\n    <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Media</span><span class=\"p\">:</span>\n        <span class=\"n\">css</span> <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        <span class=\"n\">js</span> <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>This code defines a <code class=\"docutils literal notranslate\"><span class=\"pre\">CalendarWidget</span></code>, which will be based on <code class=\"docutils literal notranslate\"><span class=\"pre\">TextInput</span></code>.\nEvery time the CalendarWidget is used on a form, that form will be directed\nto include the CSS file <code class=\"docutils literal notranslate\"><span class=\"pre\">pretty.css</span></code>, and the JavaScript files\n<code class=\"docutils literal notranslate\"><span class=\"pre\">animations.js</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">actions.js</span></code>.</p>\n<p>This static definition is converted at runtime into a widget property\nnamed <code class=\"docutils literal notranslate\"><span class=\"pre\">media</span></code>. The list of assets for a <code class=\"docutils literal notranslate\"><span class=\"pre\">CalendarWidget</span></code> instance\ncan be retrieved through this property:</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=\"n\">w</span> <span class=\"o\">=</span> <span class=\"n\">CalendarWidget</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">w</span><span class=\"o\">.</span><span class=\"n\">media</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;link href=&quot;http://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>Here’s a list of all possible <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> options. There are no required options.</p>\n<section id=\"css\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">css</span></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>A dictionary describing the CSS files required for various forms of output\nmedia.</p>\n<p>The values in the dictionary should be a tuple/list of file names. See\n<a class=\"reference internal\" href=\"#form-asset-paths\"><span class=\"std std-ref\">the section on paths</span></a> for details of how to\nspecify paths to these files.</p>\n<p>The keys in the dictionary are the output media types. These are the same\ntypes accepted by CSS files in media declarations: “all”, “aural”, “braille”,\n“embossed”, “handheld”, “print”, “projection”, “screen”, “tty” and “tv”. If\nyou need to have different stylesheets for different media types, provide\na list of CSS files for each output medium. The following example would\nprovide two CSS options – one for the screen, and one for print:</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=\"w\"> </span><span class=\"nc\">Media</span><span class=\"p\">:</span>\n    <span class=\"n\">css</span> <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>If a group of CSS files are appropriate for multiple output media types,\nthe dictionary key can be a comma separated list of output media types.\nIn the following example, TV’s and projectors will have the same media\nrequirements:</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=\"w\"> </span><span class=\"nc\">Media</span><span class=\"p\">:</span>\n    <span class=\"n\">css</span> <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>If this last CSS definition were to be rendered, it would become the following 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;http://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;http://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;http://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>\n<section id=\"js\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">js</span></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>A tuple describing the required JavaScript files. See <a class=\"reference internal\" href=\"#form-asset-paths\"><span class=\"std std-ref\">the\nsection on paths</span></a> for details of how to specify\npaths to these files.</p>\n</section>\n<section id=\"extend\">\n<h3><code class=\"docutils literal notranslate\"><span class=\"pre\">extend</span></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>A boolean defining inheritance behavior for <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> declarations.</p>\n<p>By default, any object using a static <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> definition will\ninherit all the assets associated with the parent widget. This occurs\nregardless of how the parent defines its own requirements. For\nexample, if we were to extend our basic Calendar widget from the\nexample above:</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=\"w\"> </span><span class=\"nc\">FancyCalendarWidget</span><span class=\"p\">(</span><span class=\"n\">CalendarWidget</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        <span class=\"n\">css</span> <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>        <span class=\"n\">js</span> <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><span class=\"n\">w</span> <span class=\"o\">=</span> <span class=\"n\">FancyCalendarWidget</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">w</span><span class=\"o\">.</span><span class=\"n\">media</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;link href=&quot;http://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;link href=&quot;http://static.example.com/fancy.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/whizbang.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>The FancyCalendar widget inherits all the assets from its parent\nwidget. If you don’t want <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> to be inherited in this way, add\nan <code class=\"docutils literal notranslate\"><span class=\"pre\">extend=False</span></code> declaration to the <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> declaration:</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=\"w\"> </span><span class=\"nc\">FancyCalendarWidget</span><span class=\"p\">(</span><span class=\"n\">CalendarWidget</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        <span class=\"n\">extend</span> <span class=\"o\">=</span> <span class=\"kc\">False</span>\n<span class=\"gp\">... </span>        <span class=\"n\">css</span> <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>        <span class=\"n\">js</span> <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><span class=\"n\">w</span> <span class=\"o\">=</span> <span class=\"n\">FancyCalendarWidget</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">w</span><span class=\"o\">.</span><span class=\"n\">media</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;link href=&quot;http://static.example.com/fancy.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/whizbang.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>If you require even more control over inheritance, define your assets using a\n<a class=\"reference internal\" href=\"#dynamic-property\"><span class=\"std std-ref\">dynamic property</span></a>. Dynamic properties give you\ncomplete control over which files are inherited, and which are not.</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\"><span class=\"pre\">Media</span></code> as a dynamic property<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\"><span class=\"pre\">media</span></code> property directly. This is\ndone by defining a widget property that returns an instance of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">forms.Media</span></code>.  The constructor for <code class=\"docutils literal notranslate\"><span class=\"pre\">forms.Media</span></code> accepts <code class=\"docutils literal notranslate\"><span class=\"pre\">css</span></code>\nand <code class=\"docutils literal notranslate\"><span class=\"pre\">js</span></code> keyword arguments in the same format as that used in a\nstatic media definition.</p>\n<p>For example, the static definition for our Calendar Widget could also\nbe defined in a dynamic fashion:</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=\"w\"> </span><span class=\"nc\">CalendarWidget</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">TextInput</span><span class=\"p\">):</span>\n    <span class=\"nd\">@property</span>\n    <span class=\"k\">def</span><span class=\"w\"> </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> <span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">Media</span><span class=\"p\">(</span>\n            <span class=\"n\">css</span><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> <span class=\"n\">js</span><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>See the section on <a class=\"reference internal\" href=\"#media-objects\">Media objects</a> for more details on how to construct\nreturn values for dynamic <code class=\"docutils literal notranslate\"><span class=\"pre\">media</span></code> properties.</p>\n</section>\n<section id=\"paths-in-asset-definitions\">\n<span id=\"form-asset-paths\"></span><h2>Paths in asset definitions<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>Paths as strings<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>String paths used to specify assets can be either relative or absolute. If a\npath starts with <code class=\"docutils literal notranslate\"><span class=\"pre\">/</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">http://</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">https://</span></code>, it will be\ninterpreted as an absolute path, and left as-is. All other paths will\nbe prepended with the value of the appropriate prefix. If the\n<a class=\"reference internal\" href=\"/es/5.0/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> app is installed, it will be used to serve\nassets.</p>\n<p>Whether or not you use <a class=\"reference internal\" href=\"/es/5.0/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>,  the\n<a class=\"reference internal\" href=\"/es/5.0/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_URL</span></code></a> and <a class=\"reference internal\" href=\"/es/5.0/ref/settings/#std-setting-STATIC_ROOT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_ROOT</span></code></a> settings are required to\nrender a complete web page.</p>\n<p>To find the appropriate prefix to use, Django will check if the\n<a class=\"reference internal\" href=\"/es/5.0/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_URL</span></code></a> setting is not <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code> and automatically fall back\nto using <a class=\"reference internal\" href=\"/es/5.0/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code></a>. For example, if the <a class=\"reference internal\" href=\"/es/5.0/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code></a> for\nyour site was <code class=\"docutils literal notranslate\"><span class=\"pre\">'http://uploads.example.com/'</span></code> and <a class=\"reference internal\" href=\"/es/5.0/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_URL</span></code></a>\nwas <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></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=\"w\"> </span><span class=\"nn\">django</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">forms</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">CalendarWidget</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">TextInput</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        <span class=\"n\">css</span> <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>        <span class=\"n\">js</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"s2\">&quot;animations.js&quot;</span><span class=\"p\">,</span> <span class=\"s2\">&quot;http://othersite.com/actions.js&quot;</span><span class=\"p\">]</span>\n<span class=\"gp\">...</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">w</span> <span class=\"o\">=</span> <span class=\"n\">CalendarWidget</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">w</span><span class=\"o\">.</span><span class=\"n\">media</span><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;http://uploads.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://othersite.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>But if <a class=\"reference internal\" href=\"/es/5.0/ref/settings/#std-setting-STATIC_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">STATIC_URL</span></code></a> is <code class=\"docutils literal notranslate\"><span class=\"pre\">'http://static.example.com/'</span></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=\"n\">w</span> <span class=\"o\">=</span> <span class=\"n\">CalendarWidget</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">w</span><span class=\"o\">.</span><span class=\"n\">media</span><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;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://othersite.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>Or if <a class=\"reference internal\" href=\"/es/5.0/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\">staticfiles</span></code></a> is configured using the\n<a class=\"reference internal\" href=\"/es/5.0/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage\" title=\"django.contrib.staticfiles.storage.ManifestStaticFilesStorage\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ManifestStaticFilesStorage</span></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><span class=\"n\">w</span> <span class=\"o\">=</span> <span class=\"n\">CalendarWidget</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">w</span><span class=\"o\">.</span><span class=\"n\">media</span><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;http://othersite.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n</section>\n<section id=\"paths-as-objects\">\n<h3>Paths as objects<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>Asset paths may also be given as hashable objects implementing an\n<code class=\"docutils literal notranslate\"><span class=\"pre\">__html__()</span></code> method. The <code class=\"docutils literal notranslate\"><span class=\"pre\">__html__()</span></code> method is typically added using the\n<a class=\"reference internal\" href=\"/es/5.0/ref/utils/#django.utils.html.html_safe\" title=\"django.utils.html.html_safe\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">html_safe()</span></code></a> decorator. The object is responsible for\noutputting the complete HTML <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;script&gt;</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;link&gt;</span></code> tag content:</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=\"w\"> </span><span class=\"nn\">django</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">forms</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils.html</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">html_safe</span>\n<span class=\"gp\">&gt;&gt;&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nd\">@html_safe</span>\n<span class=\"gp\">... </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">JSPath</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>        <span class=\"k\">return</span> <span class=\"s1\">&#39;&lt;script src=&quot;https://example.org/asset.js&quot; defer&gt;&#39;</span>\n<span class=\"gp\">...</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">SomeWidget</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">TextInput</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        <span class=\"n\">js</span> <span class=\"o\">=</span> <span class=\"p\">[</span><span class=\"n\">JSPath</span><span class=\"p\">()]</span>\n<span class=\"gp\">...</span>\n</code></pre></div>\n</section>\n</section>\n<section id=\"media-objects\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> objects<a class=\"heading-anchor\" href=\"#media-objects\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When you interrogate the <code class=\"docutils literal notranslate\"><span class=\"pre\">media</span></code> attribute of a widget or form, the\nvalue that is returned is a <code class=\"docutils literal notranslate\"><span class=\"pre\">forms.Media</span></code> object. As we have already\nseen, the string representation of a <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> object is the HTML\nrequired to include the relevant files in the <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;head&gt;</span></code> block of your\nHTML page.</p>\n<p>However, <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> objects have some other interesting properties.</p>\n<section id=\"subsets-of-assets\">\n<h3>Subsets of assets<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>If you only want files of a particular type, you can use the subscript\noperator to filter out a medium of interest. For example:</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=\"n\">w</span> <span class=\"o\">=</span> <span class=\"n\">CalendarWidget</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">w</span><span class=\"o\">.</span><span class=\"n\">media</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;link href=&quot;http://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://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><span class=\"n\">w</span><span class=\"o\">.</span><span class=\"n\">media</span><span class=\"p\">[</span><span class=\"s2\">&quot;css&quot;</span><span class=\"p\">])</span>\n<span class=\"go\">&lt;link href=&quot;http://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n</code></pre></div>\n<p>When you use the subscript operator, the value that is returned is a\nnew <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> object – but one that only contains the media of interest.</p>\n</section>\n<section id=\"combining-media-objects\">\n<h3>Combining <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> objects<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\"><span class=\"pre\">Media</span></code> objects can also be added together. When two <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> objects are\nadded, the resulting <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> object contains the union of the assets\nspecified by both:</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=\"w\"> </span><span class=\"nn\">django</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">forms</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">CalendarWidget</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">TextInput</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        <span class=\"n\">css</span> <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>        <span class=\"n\">js</span> <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=\"w\"> </span><span class=\"nc\">OtherWidget</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">TextInput</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        <span class=\"n\">js</span> <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><span class=\"n\">w1</span> <span class=\"o\">=</span> <span class=\"n\">CalendarWidget</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">w2</span> <span class=\"o\">=</span> <span class=\"n\">OtherWidget</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">w1</span><span class=\"o\">.</span><span class=\"n\">media</span> <span class=\"o\">+</span> <span class=\"n\">w2</span><span class=\"o\">.</span><span class=\"n\">media</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;link href=&quot;http://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://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>Order of assets<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>The order in which assets are inserted into the DOM is often important. For\nexample, you may have a script that depends on jQuery. Therefore, combining\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> objects attempts to preserve the relative order in which assets are\ndefined in each <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> class.</p>\n<p>For example:</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=\"w\"> </span><span class=\"nn\">django</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">forms</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">CalendarWidget</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">TextInput</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        <span class=\"n\">js</span> <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=\"w\"> </span><span class=\"nc\">TimeWidget</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">TextInput</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        <span class=\"n\">js</span> <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><span class=\"n\">w1</span> <span class=\"o\">=</span> <span class=\"n\">CalendarWidget</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">w2</span> <span class=\"o\">=</span> <span class=\"n\">TimeWidget</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"nb\">print</span><span class=\"p\">(</span><span class=\"n\">w1</span><span class=\"o\">.</span><span class=\"n\">media</span> <span class=\"o\">+</span> <span class=\"n\">w2</span><span class=\"o\">.</span><span class=\"n\">media</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/jQuery.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/calendar.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/time.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/noConflict.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>Combining <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> objects with assets in a conflicting order results in a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">MediaOrderConflictWarning</span></code>.</p>\n</section>\n</section>\n<section id=\"media-on-forms\">\n<h2><code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> on Forms<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>Widgets aren’t the only objects that can have <code class=\"docutils literal notranslate\"><span class=\"pre\">media</span></code> definitions –\nforms can also define <code class=\"docutils literal notranslate\"><span class=\"pre\">media</span></code>. The rules for <code class=\"docutils literal notranslate\"><span class=\"pre\">media</span></code> definitions\non forms are the same as the rules for widgets: declarations can be\nstatic or dynamic; path and inheritance rules for those declarations\nare exactly the same.</p>\n<p>Regardless of whether you define a <code class=\"docutils literal notranslate\"><span class=\"pre\">media</span></code> declaration, <em>all</em> Form\nobjects have a <code class=\"docutils literal notranslate\"><span class=\"pre\">media</span></code> property. The default value for this property\nis the result of adding the <code class=\"docutils literal notranslate\"><span class=\"pre\">media</span></code> definitions for all widgets that\nare part of the form:</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=\"w\"> </span><span class=\"nn\">django</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">forms</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">Form</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"n\">date</span> <span class=\"o\">=</span> <span class=\"n\">DateField</span><span class=\"p\">(</span><span class=\"n\">widget</span><span class=\"o\">=</span><span class=\"n\">CalendarWidget</span><span class=\"p\">)</span>\n<span class=\"gp\">... </span>    <span class=\"n\">name</span> <span class=\"o\">=</span> <span class=\"n\">CharField</span><span class=\"p\">(</span><span class=\"n\">max_length</span><span class=\"o\">=</span><span class=\"mi\">40</span><span class=\"p\">,</span> <span class=\"n\">widget</span><span class=\"o\">=</span><span class=\"n\">OtherWidget</span><span class=\"p\">)</span>\n<span class=\"gp\">...</span>\n\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">media</span>\n<span class=\"go\">&lt;link href=&quot;http://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/whizbang.js&quot;&gt;&lt;/script&gt;</span>\n</code></pre></div>\n<p>If you want to associate additional assets with a form – for example,\nCSS for form layout – add a <code class=\"docutils literal notranslate\"><span class=\"pre\">Media</span></code> declaration to the form:</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=\"w\"> </span><span class=\"nc\">ContactForm</span><span class=\"p\">(</span><span class=\"n\">forms</span><span class=\"o\">.</span><span class=\"n\">Form</span><span class=\"p\">):</span>\n<span class=\"gp\">... </span>    <span class=\"n\">date</span> <span class=\"o\">=</span> <span class=\"n\">DateField</span><span class=\"p\">(</span><span class=\"n\">widget</span><span class=\"o\">=</span><span class=\"n\">CalendarWidget</span><span class=\"p\">)</span>\n<span class=\"gp\">... </span>    <span class=\"n\">name</span> <span class=\"o\">=</span> <span class=\"n\">CharField</span><span class=\"p\">(</span><span class=\"n\">max_length</span><span class=\"o\">=</span><span class=\"mi\">40</span><span class=\"p\">,</span> <span class=\"n\">widget</span><span class=\"o\">=</span><span class=\"n\">OtherWidget</span><span class=\"p\">)</span>\n<span class=\"gp\">... </span>    <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Media</span><span class=\"p\">:</span>\n<span class=\"gp\">... </span>        <span class=\"n\">css</span> <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><span class=\"n\">f</span> <span class=\"o\">=</span> <span class=\"n\">ContactForm</span><span class=\"p\">()</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">f</span><span class=\"o\">.</span><span class=\"n\">media</span>\n<span class=\"go\">&lt;link href=&quot;http://static.example.com/pretty.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;link href=&quot;http://static.example.com/layout.css&quot; media=&quot;all&quot; rel=&quot;stylesheet&quot;&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/animations.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://static.example.com/actions.js&quot;&gt;&lt;/script&gt;</span>\n<span class=\"go\">&lt;script src=&quot;http://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":"Assets as a static definition","anchor":"assets-as-a-static-definition","children":[{"title":"css","anchor":"css","children":[]},{"title":"js","anchor":"js","children":[]},{"title":"extend","anchor":"extend","children":[]}]},{"title":"Media as a dynamic property","anchor":"media-as-a-dynamic-property","children":[]},{"title":"Paths in asset definitions","anchor":"paths-in-asset-definitions","children":[{"title":"Paths as strings","anchor":"paths-as-strings","children":[]},{"title":"Paths as objects","anchor":"paths-as-objects","children":[]}]},{"title":"Media objects","anchor":"media-objects","children":[{"title":"Subsets of assets","anchor":"subsets-of-assets","children":[]},{"title":"Combining Media objects","anchor":"combining-media-objects","children":[]},{"title":"Order of assets","anchor":"order-of-assets","children":[]}]},{"title":"Media on Forms","anchor":"media-on-forms","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Using Django","url":"/es/5.0/topics/"},{"docname":"topics/forms/index","title":"Working with forms","url":"/es/5.0/topics/forms/"}],"prev":{"docname":"topics/forms/modelforms","title":"Creating forms from models","url":"/es/5.0/topics/forms/modelforms/"},"next":{"docname":"topics/templates","title":"Plantillas","url":"/es/5.0/topics/templates/"},"formats":{"html":"/es/5.0/topics/forms/media/","markdown":"/es/5.0/topics/forms/media.md","json":"/es/5.0/topics/forms/media.json"},"source":"https://github.com/django/django/blob/stable/5.0.x/docs/topics/forms/media.txt","official":"https://docs.djangoproject.com/es/5.0/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","1.11","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}