Django

Code

Changeset 8201

Show
Ignore:
Timestamp:
08/03/08 14:04:32 (5 months ago)
Author:
gwilson
Message:

Removed trailing whitespace.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/forms.txt

    r8110 r8201  
    4040    * **Media** -- A definition of the CSS and JavaScript resources that are 
    4141      required to render a form. 
    42        
     42 
    4343The library is decoupled from the other Django components, such as the database 
    4444layer, views and templates. It relies only on Django settings, a couple of 
     
    11991199Takes one extra argument, ``choices``, which is an iterable (e.g., a list or 
    12001200tuple) of 2-tuples to use as choices for this field. This argument accepts 
    1201 the same formats as the ``choices`` argument to a model field. See the  
     1201the same formats as the ``choices`` argument to a model field. See the 
    12021202`model API documentation on choices`_ for more details. 
    12031203 
     
    13251325    Argument        Required?   Description 
    13261326    ==============  ==========  =============================================== 
    1327     ``path``        Yes         The absolute path to the directory whose  
    1328                                 contents you want listed. This directory must  
     1327    ``path``        Yes         The absolute path to the directory whose 
     1328                                contents you want listed. This directory must 
    13291329                                exist. 
    1330                              
     1330 
    13311331    ``recursive``   No          If ``False`` (the default) only the direct 
    13321332                                contents of ``path`` will be offered as choices. 
     
    13341334                                into recursively and all descendants will be 
    13351335                                listed as choices. 
    1336                                  
     1336 
    13371337    ``match``       No          A regular expression pattern; only files with 
    13381338                                names matching this expression will be allowed 
     
    13401340    ==============  ==========  =============================================== 
    13411341 
    1342 ``FloatField``  
    1343 ~~~~~~~~~~~~~~  
    1344  
    1345     * Default widget: ``TextInput``  
    1346     * Empty value: ``None``  
    1347     * Normalizes to: A Python float.  
    1348     * Validates that the given value is an float. Leading and trailing  
    1349       whitespace is allowed, as in Python's ``float()`` function.  
    1350     * Error message keys: ``required``, ``invalid``, ``max_value``,  
    1351       ``min_value``  
    1352           
    1353 Takes two optional arguments for validation, ``max_value`` and ``min_value``.  
     1342``FloatField`` 
     1343~~~~~~~~~~~~~~ 
     1344 
     1345    * Default widget: ``TextInput`` 
     1346    * Empty value: ``None`` 
     1347    * Normalizes to: A Python float. 
     1348    * Validates that the given value is an float. Leading and trailing 
     1349      whitespace is allowed, as in Python's ``float()`` function. 
     1350    * Error message keys: ``required``, ``invalid``, ``max_value``, 
     1351      ``min_value`` 
     1352 
     1353Takes two optional arguments for validation, ``max_value`` and ``min_value``. 
    13541354These control the range of values permitted in the field. 
    13551355 
     
    14111411Takes one extra argument, ``choices``, which is an iterable (e.g., a list or 
    14121412tuple) of 2-tuples to use as choices for this field. This argument accepts 
    1413 the same formats as the ``choices`` argument to a model field. See the  
     1413the same formats as the ``choices`` argument to a model field. See the 
    14141414`model API documentation on choices`_ for more details. 
    14151415 
     
    18381838===== 
    18391839 
    1840 Rendering an attractive and easy-to-use web form requires more than just  
    1841 HTML - it also requires CSS stylesheets, and if you want to use fancy  
    1842 "Web2.0" widgets, you may also need to include some JavaScript on each  
    1843 page. The exact combination of CSS and JavaScript that is required for  
     1840Rendering an attractive and easy-to-use web form requires more than just 
     1841HTML - it also requires CSS stylesheets, and if you want to use fancy 
     1842"Web2.0" widgets, you may also need to include some JavaScript on each 
     1843page. The exact combination of CSS and JavaScript that is required for 
    18441844any given page will depend upon the widgets that are in use on that page. 
    18451845 
    1846 This is where Django media definitions come in. Django allows you to  
     1846This is where Django media definitions come in. Django allows you to 
    18471847associate different media files with the forms and widgets that require 
    1848 that media. For example, if you want to use a calendar to render DateFields,  
    1849 you can define a custom Calendar widget. This widget can then be associated  
    1850 with the CSS and JavaScript that is required to render the calendar. When  
     1848that media. For example, if you want to use a calendar to render DateFields, 
     1849you can define a custom Calendar widget. This widget can then be associated 
     1850with the CSS and JavaScript that is required to render the calendar. When 
    18511851the Calendar widget is used on a form, Django is able to identify the CSS and 
    18521852JavaScript files that are required, and provide the list of file names 
     
    18591859    media requirements, and the Django Admin uses the custom widgets 
    18601860    in place of the Django defaults. The Admin templates will only include 
    1861     those media files that are required to render the widgets on any  
     1861    those media files that are required to render the widgets on any 
    18621862    given page. 
    1863      
     1863 
    18641864    If you like the widgets that the Django Admin application uses, 
    18651865    feel free to use them in your own application! They're all stored 
     
    18691869 
    18701870    Many JavaScript toolkits exist, and many of them include widgets (such 
    1871     as calendar widgets) that can be used to enhance your application.  
     1871    as calendar widgets) that can be used to enhance your application. 
    18721872    Django has deliberately avoided blessing any one JavaScript toolkit. 
    1873     Each toolkit has its own relative strengths and weaknesses - use  
     1873    Each toolkit has its own relative strengths and weaknesses - use 
    18741874    whichever toolkit suits your requirements. Django is able to integrate 
    1875     with any JavaScript toolkit.  
     1875    with any JavaScript toolkit. 
    18761876 
    18771877Media as a static definition 
     
    18801880The easiest way to define media is as a static definition. Using this method, 
    18811881the media declaration is an inner class. The properties of the inner class 
    1882 define the media requirements.  
     1882define the media requirements. 
    18831883 
    18841884Here's a simple example:: 
     
    18921892 
    18931893This code defines a ``CalendarWidget``, which will be based on ``TextInput``. 
    1894 Every time the CalendarWidget is used on a form, that form will be directed  
    1895 to include the CSS file ``pretty.css``, and the JavaScript files  
    1896 ``animations.js`` and ``actions.js``.  
     1894Every time the CalendarWidget is used on a form, that form will be directed 
     1895to include the CSS file ``pretty.css``, and the JavaScript files 
     1896``animations.js`` and ``actions.js``. 
    18971897 
    18981898This static media definition is converted at runtime into a widget property 
    1899 named ``media``. The media for a CalendarWidget instance can be retrieved  
     1899named ``media``. The media for a CalendarWidget instance can be retrieved 
    19001900through this property:: 
    19011901 
     
    19111911~~~~~~~ 
    19121912 
    1913 A dictionary describing the CSS files required for various forms of output  
    1914 media.  
     1913A dictionary describing the CSS files required for various forms of output 
     1914media. 
    19151915 
    19161916The values in the dictionary should be a tuple/list of file names. See 
    1917 `the section on media paths`_ for details of how to specify paths to media  
     1917`the section on media paths`_ for details of how to specify paths to media 
    19181918files. 
    19191919 
    19201920.. _the section on media paths: `Paths in media definitions`_ 
    19211921 
    1922 The keys in the dictionary are the output media types. These are the same  
     1922The keys in the dictionary are the output media types. These are the same 
    19231923types accepted by CSS files in media declarations: 'all', 'aural', 'braille', 
    19241924'embossed', 'handheld', 'print', 'projection', 'screen', 'tty' and 'tv'. If 
     
    19331933        } 
    19341934 
    1935 If a group of CSS files are appropriate for multiple output media types,  
    1936 the dictionary key can be a comma separated list of output media types.  
    1937 In the following example, TV's and projectors will have the same media  
     1935If a group of CSS files are appropriate for multiple output media types, 
     1936the dictionary key can be a comma separated list of output media types. 
     1937In the following example, TV's and projectors will have the same media 
    19381938requirements:: 
    19391939 
     
    19441944            'print': ('newspaper.css',) 
    19451945        } 
    1946      
     1946 
    19471947If this last CSS definition were to be rendered, it would become the following HTML:: 
    19481948 
     
    19501950    <link href="http://media.example.com/lo_res.css" type="text/css" media="tv,projector" rel="stylesheet" /> 
    19511951    <link href="http://media.example.com/newspaper.css" type="text/css" media="print" rel="stylesheet" /> 
    1952                   
     1952 
    19531953``js`` 
    19541954~~~~~~ 
    19551955 
    19561956A tuple describing the required JavaScript files. See 
    1957 `the section on media paths`_ for details of how to specify paths to media  
     1957`the section on media paths`_ for details of how to specify paths to media 
    19581958files. 
    19591959 
    1960 ``extend``    
     1960``extend`` 
    19611961~~~~~~~~~~ 
    19621962 
    1963 A boolean defining inheritance behavior for media declarations.  
     1963A boolean defining inheritance behavior for media declarations. 
    19641964 
    19651965By default, any object using a static media definition will inherit all the 
    1966 media associated with the parent widget. This occurs regardless of how the  
     1966media associated with the parent widget. This occurs regardless of how the 
    19671967parent defines its media requirements. For example, if we were to extend our 
    1968 basic Calendar widget from the example above::  
     1968basic Calendar widget from the example above:: 
    19691969 
    19701970    class FancyCalendarWidget(CalendarWidget): 
     
    19831983    <script type="text/javascript" src="http://media.example.com/whizbang.js"></script> 
    19841984 
    1985 The FancyCalendar widget inherits all the media from it's parent widget. If  
     1985The FancyCalendar widget inherits all the media from it's parent widget. If 
    19861986you don't want media to be inherited in this way, add an ``extend=False`` 
    19871987declaration to the media declaration:: 
     
    19891989    class FancyCalendar(Calendar): 
    19901990        class Media: 
    1991             extend = False  
     1991            extend = False 
    19921992            css = { 
    19931993                'all': ('fancy.css',) 
     
    20002000    <script type="text/javascript" src="http://media.example.com/whizbang.js"></script> 
    20012001 
    2002 If you require even more control over media inheritance, define your media  
     2002If you require even more control over media inheritance, define your media 
    20032003using a `dynamic property`_. Dynamic properties give you complete control over 
    20042004which media files are inherited, and which are not. 
    20052005 
    20062006.. _dynamic property: `Media as a dynamic property`_ 
    2007      
     2007 
    20082008Media as a dynamic property 
    20092009--------------------------- 
     
    20122012requirements, you can define the media property directly. This is done 
    20132013by defining a model property that returns an instance of ``forms.Media``. 
    2014 The constructor for ``forms.Media`` accepts ``css`` and ``js`` keyword  
     2014The constructor for ``forms.Media`` accepts ``css`` and ``js`` keyword 
    20152015arguments in the same format as that used in a static media definition. 
    20162016 
     
    20302030-------------------------- 
    20312031 
    2032 Paths used to specify media can be either relative or absolute. If a path  
     2032Paths used to specify media can be either relative or absolute. If a path 
    20332033starts with '/', 'http://' or 'https://', it will be interpreted as an absolute 
    20342034path, and left as-is. All other paths will be prepended with the value of 
     
    20542054When you interrogate the media attribute of a widget or form, the value that 
    20552055is returned is a ``forms.Media`` object. As we have already seen, the string 
    2056 representation of a Media object is the HTML required to include media  
    2057 in the ``<head>`` block of your HTML page.  
     2056representation of a Media object is the HTML required to include media 
     2057in the ``<head>`` block of your HTML page. 
    20582058 
    20592059However, Media objects have some other interesting properties. 
     
    20622062~~~~~~~~~~~~~ 
    20632063 
    2064 If you only want media of a particular type, you can use the subscript operator  
     2064If you only want media of a particular type, you can use the subscript operator 
    20652065to filter out a medium of interest. For example:: 
    20662066 
     
    20742074    <link href="http://media.example.com/pretty.css" type="text/css" media="all" rel="stylesheet" /> 
    20752075 
    2076 When you use the subscript operator, the value that is returned is a new  
     2076When you use the subscript operator, the value that is returned is a new 
    20772077Media object -- but one that only contains the media of interest. 
    20782078 
     
    21012101    <script type="text/javascript" src="http://media.example.com/actions.js"></script> 
    21022102    <script type="text/javascript" src="http://media.example.com/whizbang.js"></script> 
    2103      
     2103 
    21042104Media on Forms 
    21052105-------------- 
     
    21102110path and inheritance rules for those declarations are exactly the same. 
    21112111 
    2112 Regardless of whether you define a media declaration, *all* Form objects  
    2113 have a media property. The default value for this property is the result  
     2112Regardless of whether you define a media declaration, *all* Form objects 
     2113have a media property. The default value for this property is the result 
    21142114of adding the media definitions for all widgets that are part of the form:: 
    21152115 
     
    21252125    <script type="text/javascript" src="http://media.example.com/whizbang.js"></script> 
    21262126 
    2127 If you want to associate additional media with a form -- for example, CSS for form  
     2127If you want to associate additional media with a form -- for example, CSS for form 
    21282128layout -- simply add a media declaration to the form:: 
    21292129 
     
    21362136                'all': ('layout.css',) 
    21372137            } 
    2138              
     2138 
    21392139    >>> f = ContactForm() 
    21402140    >>> f.media 
     
    21932193    ...      'pub_date': datetime.date.today()}, 
    21942194    ... ]) 
    2195      
     2195 
    21962196    >>> for form in formset.forms: 
    21972197    ...     print form.as_table() 
     
    22822282 
    22832283    >>> from django.forms.formsets import BaseFormSet 
    2284      
     2284 
    22852285    >>> class BaseArticleFormSet(BaseFormSet): 
    22862286    ...     def clean(self): 
    22872287    ...         raise forms.ValidationError, u'An error occured.' 
    2288      
     2288 
    22892289    >>> ArticleFormSet = formset_factory(ArticleForm, formset=BaseArticleFormSet) 
    22902290    >>> formset = ArticleFormSet({}) 
     
    23492349    ...     'form-2-ORDER': u'0', 
    23502350    ... } 
    2351      
     2351 
    23522352    >>> formset = ArticleFormSet(data, initial=[ 
    23532353    ...     {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)}, 
     
    24042404    ...     'form-2-DELETE': u'', 
    24052405    ... } 
    2406      
     2406 
    24072407    >>> formset = ArticleFormSet(data, initial=[ 
    24082408    ...     {'title': u'Article #1', 'pub_date': datetime.date(2008, 5, 10)},