2013-06-20 14:51:20 +08:00
|
|
|
Form Assets (the ``Media`` class)
|
|
|
|
=================================
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-10-09 16:12:50 +08:00
|
|
|
Rendering an attractive and easy-to-use Web form requires more than just
|
2008-08-24 06:25:40 +08:00
|
|
|
HTML - it also requires CSS stylesheets, and if you want to use fancy
|
|
|
|
"Web2.0" widgets, you may also need to include some JavaScript on each
|
|
|
|
page. The exact combination of CSS and JavaScript that is required for
|
|
|
|
any given page will depend upon the widgets that are in use on that page.
|
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
This is where asset definitions come in. Django allows you to
|
|
|
|
associate different files -- like stylesheets and scripts -- with the
|
|
|
|
forms and widgets that require those assets. For example, if you want
|
|
|
|
to use a calendar to render DateFields, you can define a custom
|
|
|
|
Calendar widget. This widget can then be associated with the CSS and
|
|
|
|
JavaScript that is required to render the calendar. When the Calendar
|
|
|
|
widget is used on a form, Django is able to identify the CSS and
|
2008-08-24 06:25:40 +08:00
|
|
|
JavaScript files that are required, and provide the list of file names
|
2010-10-09 16:12:50 +08:00
|
|
|
in a form suitable for easy inclusion on your Web page.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
.. admonition:: Assets and Django Admin
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
The Django Admin application defines a number of customized
|
|
|
|
widgets for calendars, filtered selections, and so on. These
|
|
|
|
widgets define asset requirements, and the Django Admin uses the
|
|
|
|
custom widgets in place of the Django defaults. The Admin
|
|
|
|
templates will only include those files that are required to
|
|
|
|
render the widgets on any given page.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
If you like the widgets that the Django Admin application uses,
|
|
|
|
feel free to use them in your own application! They're all stored
|
|
|
|
in ``django.contrib.admin.widgets``.
|
|
|
|
|
|
|
|
.. admonition:: Which JavaScript toolkit?
|
|
|
|
|
|
|
|
Many JavaScript toolkits exist, and many of them include widgets (such
|
|
|
|
as calendar widgets) that can be used to enhance your application.
|
|
|
|
Django has deliberately avoided blessing any one JavaScript toolkit.
|
|
|
|
Each toolkit has its own relative strengths and weaknesses - use
|
|
|
|
whichever toolkit suits your requirements. Django is able to integrate
|
|
|
|
with any JavaScript toolkit.
|
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
.. _assets-as-a-static-definition:
|
2012-09-12 18:59:19 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
Assets as a static definition
|
|
|
|
-----------------------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
The easiest way to define assets is as a static definition. Using this
|
|
|
|
method, the declaration is an inner ``Media`` class. The properties of the
|
|
|
|
inner class define the requirements.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Here's a simple example::
|
|
|
|
|
2013-06-09 13:23:48 +08:00
|
|
|
from django import forms
|
2013-05-19 17:15:35 +08:00
|
|
|
|
2008-08-24 06:25:40 +08:00
|
|
|
class CalendarWidget(forms.TextInput):
|
|
|
|
class Media:
|
|
|
|
css = {
|
|
|
|
'all': ('pretty.css',)
|
|
|
|
}
|
|
|
|
js = ('animations.js', 'actions.js')
|
|
|
|
|
|
|
|
This code defines a ``CalendarWidget``, which will be based on ``TextInput``.
|
|
|
|
Every time the CalendarWidget is used on a form, that form will be directed
|
|
|
|
to include the CSS file ``pretty.css``, and the JavaScript files
|
|
|
|
``animations.js`` and ``actions.js``.
|
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
This static definition is converted at runtime into a widget property
|
|
|
|
named ``media``. The list of assets for a ``CalendarWidget`` instance
|
|
|
|
can be retrieved through this property::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
>>> w = CalendarWidget()
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(w.media)
|
2012-08-24 20:23:50 +08:00
|
|
|
<link href="http://static.example.com/pretty.css" type="text/css" media="all" rel="stylesheet" />
|
|
|
|
<script type="text/javascript" src="http://static.example.com/animations.js"></script>
|
|
|
|
<script type="text/javascript" src="http://static.example.com/actions.js"></script>
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
Here's a list of all possible ``Media`` options. There are no required options.
|
|
|
|
|
|
|
|
``css``
|
|
|
|
~~~~~~~
|
|
|
|
|
|
|
|
A dictionary describing the CSS files required for various forms of output
|
|
|
|
media.
|
|
|
|
|
|
|
|
The values in the dictionary should be a tuple/list of file names. See
|
2013-06-20 14:51:20 +08:00
|
|
|
:ref:`the section on paths <form-asset-paths>` for details of how to
|
|
|
|
specify paths to these files.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
The keys in the dictionary are the output media types. These are the same
|
|
|
|
types accepted by CSS files in media declarations: 'all', 'aural', 'braille',
|
|
|
|
'embossed', 'handheld', 'print', 'projection', 'screen', 'tty' and 'tv'. If
|
|
|
|
you need to have different stylesheets for different media types, provide
|
|
|
|
a list of CSS files for each output medium. The following example would
|
|
|
|
provide two CSS options -- one for the screen, and one for print::
|
|
|
|
|
|
|
|
class Media:
|
|
|
|
css = {
|
|
|
|
'screen': ('pretty.css',),
|
|
|
|
'print': ('newspaper.css',)
|
|
|
|
}
|
|
|
|
|
|
|
|
If a group of CSS files are appropriate for multiple output media types,
|
|
|
|
the dictionary key can be a comma separated list of output media types.
|
|
|
|
In the following example, TV's and projectors will have the same media
|
|
|
|
requirements::
|
|
|
|
|
|
|
|
class Media:
|
|
|
|
css = {
|
|
|
|
'screen': ('pretty.css',),
|
|
|
|
'tv,projector': ('lo_res.css',),
|
|
|
|
'print': ('newspaper.css',)
|
|
|
|
}
|
|
|
|
|
|
|
|
If this last CSS definition were to be rendered, it would become the following HTML::
|
|
|
|
|
2012-08-24 20:23:50 +08:00
|
|
|
<link href="http://static.example.com/pretty.css" type="text/css" media="screen" rel="stylesheet" />
|
|
|
|
<link href="http://static.example.com/lo_res.css" type="text/css" media="tv,projector" rel="stylesheet" />
|
|
|
|
<link href="http://static.example.com/newspaper.css" type="text/css" media="print" rel="stylesheet" />
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
``js``
|
|
|
|
~~~~~~
|
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
A tuple describing the required JavaScript files. See :ref:`the
|
|
|
|
section on paths <form-asset-paths>` for details of how to specify
|
|
|
|
paths to these files.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
``extend``
|
|
|
|
~~~~~~~~~~
|
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
A boolean defining inheritance behavior for ``Media`` declarations.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
By default, any object using a static ``Media`` definition will
|
|
|
|
inherit all the assets associated with the parent widget. This occurs
|
|
|
|
regardless of how the parent defines its own requirements. For
|
|
|
|
example, if we were to extend our basic Calendar widget from the
|
|
|
|
example above::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-11-17 23:36:58 +08:00
|
|
|
>>> class FancyCalendarWidget(CalendarWidget):
|
2010-11-18 00:08:28 +08:00
|
|
|
... class Media:
|
|
|
|
... css = {
|
|
|
|
... 'all': ('fancy.css',)
|
|
|
|
... }
|
|
|
|
... js = ('whizbang.js',)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
>>> w = FancyCalendarWidget()
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(w.media)
|
2012-08-24 20:23:50 +08:00
|
|
|
<link href="http://static.example.com/pretty.css" type="text/css" media="all" rel="stylesheet" />
|
|
|
|
<link href="http://static.example.com/fancy.css" type="text/css" media="all" rel="stylesheet" />
|
|
|
|
<script type="text/javascript" src="http://static.example.com/animations.js"></script>
|
|
|
|
<script type="text/javascript" src="http://static.example.com/actions.js"></script>
|
|
|
|
<script type="text/javascript" src="http://static.example.com/whizbang.js"></script>
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
The FancyCalendar widget inherits all the assets from its parent
|
|
|
|
widget. If you don't want ``Media`` to be inherited in this way, add
|
|
|
|
an ``extend=False`` declaration to the ``Media`` declaration::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-11-17 23:36:58 +08:00
|
|
|
>>> class FancyCalendarWidget(CalendarWidget):
|
2010-11-18 00:08:28 +08:00
|
|
|
... class Media:
|
|
|
|
... extend = False
|
|
|
|
... css = {
|
|
|
|
... 'all': ('fancy.css',)
|
|
|
|
... }
|
|
|
|
... js = ('whizbang.js',)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
>>> w = FancyCalendarWidget()
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(w.media)
|
2012-08-24 20:23:50 +08:00
|
|
|
<link href="http://static.example.com/fancy.css" type="text/css" media="all" rel="stylesheet" />
|
|
|
|
<script type="text/javascript" src="http://static.example.com/whizbang.js"></script>
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
If you require even more control over inheritance, define your assets using a
|
|
|
|
:ref:`dynamic property <dynamic-property>`. Dynamic properties give you
|
|
|
|
complete control over which files are inherited, and which are not.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2012-09-12 18:59:19 +08:00
|
|
|
.. _dynamic-property:
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
``Media`` as a dynamic property
|
|
|
|
-------------------------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
If you need to perform some more sophisticated manipulation of asset
|
|
|
|
requirements, you can define the ``media`` property directly. This is
|
|
|
|
done by defining a widget property that returns an instance of
|
|
|
|
``forms.Media``. The constructor for ``forms.Media`` accepts ``css``
|
|
|
|
and ``js`` keyword arguments in the same format as that used in a
|
|
|
|
static media definition.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
For example, the static definition for our Calendar Widget could also
|
|
|
|
be defined in a dynamic fashion::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
class CalendarWidget(forms.TextInput):
|
|
|
|
def _media(self):
|
|
|
|
return forms.Media(css={'all': ('pretty.css',)},
|
|
|
|
js=('animations.js', 'actions.js'))
|
|
|
|
media = property(_media)
|
|
|
|
|
|
|
|
See the section on `Media objects`_ for more details on how to construct
|
2013-06-20 14:51:20 +08:00
|
|
|
return values for dynamic ``media`` properties.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
.. _form-asset-paths:
|
2010-11-17 23:36:26 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
Paths in asset definitions
|
2008-08-24 06:25:40 +08:00
|
|
|
--------------------------
|
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
Paths used to specify assets can be either relative or absolute. If a
|
|
|
|
path starts with ``/``, ``http://`` or ``https://``, it will be
|
|
|
|
interpreted as an absolute path, and left as-is. All other paths will
|
|
|
|
be prepended with the value of the appropriate prefix.
|
2010-11-17 23:36:26 +08:00
|
|
|
|
|
|
|
As part of the introduction of the
|
|
|
|
:doc:`staticfiles app </ref/contrib/staticfiles>` two new settings were added
|
2011-01-31 01:23:25 +08:00
|
|
|
to refer to "static files" (images, CSS, Javascript, etc.) that are needed
|
2010-11-17 23:36:26 +08:00
|
|
|
to render a complete web page: :setting:`STATIC_URL` and :setting:`STATIC_ROOT`.
|
|
|
|
|
|
|
|
To find the appropriate prefix to use, Django will check if the
|
|
|
|
:setting:`STATIC_URL` setting is not ``None`` and automatically fall back
|
|
|
|
to using :setting:`MEDIA_URL`. For example, if the :setting:`MEDIA_URL` for
|
|
|
|
your site was ``'http://uploads.example.com/'`` and :setting:`STATIC_URL`
|
|
|
|
was ``None``::
|
|
|
|
|
2013-05-19 17:15:35 +08:00
|
|
|
>>> from django import forms
|
2010-11-17 23:36:26 +08:00
|
|
|
>>> class CalendarWidget(forms.TextInput):
|
2010-11-18 00:08:28 +08:00
|
|
|
... class Media:
|
|
|
|
... css = {
|
|
|
|
... 'all': ('/css/pretty.css',),
|
|
|
|
... }
|
|
|
|
... js = ('animations.js', 'http://othersite.com/actions.js')
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-11-17 23:36:26 +08:00
|
|
|
>>> w = CalendarWidget()
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(w.media)
|
2010-11-17 23:36:26 +08:00
|
|
|
<link href="/css/pretty.css" type="text/css" media="all" rel="stylesheet" />
|
|
|
|
<script type="text/javascript" src="http://uploads.example.com/animations.js"></script>
|
|
|
|
<script type="text/javascript" src="http://othersite.com/actions.js"></script>
|
|
|
|
|
|
|
|
But if :setting:`STATIC_URL` is ``'http://static.example.com/'``::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
>>> w = CalendarWidget()
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(w.media)
|
2008-08-24 06:25:40 +08:00
|
|
|
<link href="/css/pretty.css" type="text/css" media="all" rel="stylesheet" />
|
2010-11-17 23:36:26 +08:00
|
|
|
<script type="text/javascript" src="http://static.example.com/animations.js"></script>
|
2008-08-24 06:25:40 +08:00
|
|
|
<script type="text/javascript" src="http://othersite.com/actions.js"></script>
|
|
|
|
|
2010-11-17 23:36:26 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
``Media`` objects
|
|
|
|
-----------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
When you interrogate the ``media`` attribute of a widget or form, the
|
|
|
|
value that is returned is a ``forms.Media`` object. As we have already
|
|
|
|
seen, the string representation of a ``Media`` object is the HTML
|
|
|
|
required to include the relevant files in the ``<head>`` block of your
|
|
|
|
HTML page.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
However, ``Media`` objects have some other interesting properties.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
Subsets of assets
|
|
|
|
~~~~~~~~~~~~~~~~~
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
If you only want files of a particular type, you can use the subscript
|
|
|
|
operator to filter out a medium of interest. For example::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
>>> w = CalendarWidget()
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(w.media)
|
2012-08-24 20:23:50 +08:00
|
|
|
<link href="http://static.example.com/pretty.css" type="text/css" media="all" rel="stylesheet" />
|
|
|
|
<script type="text/javascript" src="http://static.example.com/animations.js"></script>
|
|
|
|
<script type="text/javascript" src="http://static.example.com/actions.js"></script>
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-07-19 16:48:38 +08:00
|
|
|
>>> print(w.media['css'])
|
2012-08-24 20:23:50 +08:00
|
|
|
<link href="http://static.example.com/pretty.css" type="text/css" media="all" rel="stylesheet" />
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
When you use the subscript operator, the value that is returned is a
|
|
|
|
new ``Media`` object -- but one that only contains the media of interest.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
Combining ``Media`` objects
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
``Media`` objects can also be added together. When two ``Media`` objects are
|
|
|
|
added, the resulting ``Media`` object contains the union of the assets
|
|
|
|
specified by both::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-05-19 17:15:35 +08:00
|
|
|
>>> from django import forms
|
2010-11-17 23:36:58 +08:00
|
|
|
>>> class CalendarWidget(forms.TextInput):
|
2010-11-18 00:08:28 +08:00
|
|
|
... class Media:
|
|
|
|
... css = {
|
|
|
|
... 'all': ('pretty.css',)
|
|
|
|
... }
|
|
|
|
... js = ('animations.js', 'actions.js')
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-11-17 23:36:58 +08:00
|
|
|
>>> class OtherWidget(forms.TextInput):
|
2010-11-18 00:08:28 +08:00
|
|
|
... class Media:
|
|
|
|
... js = ('whizbang.js',)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
>>> w1 = CalendarWidget()
|
|
|
|
>>> w2 = OtherWidget()
|
2012-04-29 00:02:01 +08:00
|
|
|
>>> print(w1.media + w2.media)
|
2012-08-24 20:23:50 +08:00
|
|
|
<link href="http://static.example.com/pretty.css" type="text/css" media="all" rel="stylesheet" />
|
|
|
|
<script type="text/javascript" src="http://static.example.com/animations.js"></script>
|
|
|
|
<script type="text/javascript" src="http://static.example.com/actions.js"></script>
|
|
|
|
<script type="text/javascript" src="http://static.example.com/whizbang.js"></script>
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
``Media`` on Forms
|
|
|
|
------------------
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
Widgets aren't the only objects that can have ``media`` definitions --
|
|
|
|
forms can also define ``media``. The rules for ``media`` definitions
|
|
|
|
on forms are the same as the rules for widgets: declarations can be
|
|
|
|
static or dynamic; path and inheritance rules for those declarations
|
|
|
|
are exactly the same.
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
Regardless of whether you define a ``media`` declaration, *all* Form
|
|
|
|
objects have a ``media`` property. The default value for this property
|
|
|
|
is the result of adding the ``media`` definitions for all widgets that
|
|
|
|
are part of the form::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-05-19 17:15:35 +08:00
|
|
|
>>> from django import forms
|
2010-11-17 23:36:58 +08:00
|
|
|
>>> class ContactForm(forms.Form):
|
2010-11-18 00:08:28 +08:00
|
|
|
... date = DateField(widget=CalendarWidget)
|
|
|
|
... name = CharField(max_length=40, widget=OtherWidget)
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
>>> f = ContactForm()
|
|
|
|
>>> f.media
|
2012-08-24 20:23:50 +08:00
|
|
|
<link href="http://static.example.com/pretty.css" type="text/css" media="all" rel="stylesheet" />
|
|
|
|
<script type="text/javascript" src="http://static.example.com/animations.js"></script>
|
|
|
|
<script type="text/javascript" src="http://static.example.com/actions.js"></script>
|
|
|
|
<script type="text/javascript" src="http://static.example.com/whizbang.js"></script>
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2013-06-20 14:51:20 +08:00
|
|
|
If you want to associate additional assets with a form -- for example,
|
|
|
|
CSS for form layout -- simply add a ``Media`` declaration to the
|
|
|
|
form::
|
2008-08-24 06:25:40 +08:00
|
|
|
|
2010-11-17 23:36:58 +08:00
|
|
|
>>> class ContactForm(forms.Form):
|
2010-11-18 00:08:28 +08:00
|
|
|
... date = DateField(widget=CalendarWidget)
|
|
|
|
... name = CharField(max_length=40, widget=OtherWidget)
|
|
|
|
...
|
|
|
|
... class Media:
|
|
|
|
... css = {
|
|
|
|
... 'all': ('layout.css',)
|
|
|
|
... }
|
2008-08-24 06:25:40 +08:00
|
|
|
|
|
|
|
>>> f = ContactForm()
|
|
|
|
>>> f.media
|
2012-08-24 20:23:50 +08:00
|
|
|
<link href="http://static.example.com/pretty.css" type="text/css" media="all" rel="stylesheet" />
|
|
|
|
<link href="http://static.example.com/layout.css" type="text/css" media="all" rel="stylesheet" />
|
|
|
|
<script type="text/javascript" src="http://static.example.com/animations.js"></script>
|
|
|
|
<script type="text/javascript" src="http://static.example.com/actions.js"></script>
|
|
|
|
<script type="text/javascript" src="http://static.example.com/whizbang.js"></script>
|