Django

Code

Changeset 5224

Show
Ignore:
Timestamp:
05/14/07 02:16:27 (2 years ago)
Author:
mtredinnick
Message:

unicode: Merged from trunk up to [5222] (want to use the extra tests).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/unicode

    • Property svnmerge-integrated changed from /django/trunk:1-5213 to /django/trunk:1-5223
  • django/branches/unicode/django/test/testcases.py

    r5214 r5224  
    11import re, doctest, unittest 
    2 import sys 
    32from urlparse import urlparse 
    43from django.db import transaction 
  • django/branches/unicode/docs/databrowse.txt

    r5054 r5224  
    4545       some point. A good place for it is in your URLconf file (``urls.py``). 
    4646 
    47     3. Add the following line to your URLconf:: 
     47    3. Change your URLconf to import the ``databrowse`` module:: 
     48 
     49           from django.contrib import databrowse 
     50 
     51       ...and add the following line to your URLconf:: 
    4852 
    4953           (r'^databrowse/(.*)', databrowse.site.root), 
  • django/branches/unicode/docs/newforms.txt

    r5214 r5224  
    1010============== 
    1111 
    12 ``django.newforms`` currently is only available in Django beginning 
    13 with the 0.96 release.  the Django development version -- i.e., it's 
    14 not available in the Django 0.95 release. For the next Django release, 
    15 our plan is to do the following: 
    16  
    17     * As of revision [4208], we've copied the current ``django.forms`` to 
    18       ``django.oldforms``. This allows you to upgrade your code *now* rather 
    19       than waiting for the backwards-incompatible change and rushing to fix 
    20       your code after the fact. Just change your import statements like this:: 
     12``django.newforms`` is new in Django's 0.96 release, but, as it won't be new 
     13forever, we plan to rename it to ``django.forms`` in the future. The current 
     14``django.forms`` package will be available as ``django.oldforms`` until Django 
     151.0, when we plan to remove it for good. 
     16 
     17That has direct repercussions on the forward compatibility of your code. Please 
     18read the following migration plan and code accordingly: 
     19 
     20    * The old forms framework (the current ``django.forms``) has been copied to 
     21      ``django.oldforms``. Thus, you can start upgrading your code *now*, 
     22      rather than waiting for the future backwards-incompatible change, by 
     23      changing your import statements like this:: 
    2124 
    2225          from django import forms             # old 
    2326          from django import oldforms as forms # new 
    2427 
    25     * At an undecided future date, we will move the current ``django.newforms`` 
    26       to ``django.forms``. This will be a backwards-incompatible change, and 
    27       anybody who is still using the old version of ``django.forms`` at that 
    28       time will need to change their import statements, as described in the 
    29       previous bullet. 
     28    * In the next Django release (0.97), we will move the current 
     29      ``django.newforms`` to ``django.forms``. This will be a 
     30      backwards-incompatible change, and anybody who is still using the old 
     31      version of ``django.forms`` at that time will need to change their import 
     32      statements, as described in the previous bullet. 
    3033 
    3134    * We will remove ``django.oldforms`` in the release *after* the next Django 
    32       release -- the release that comes after the release in which we're 
    33       creating the new ``django.forms``. 
     35      release -- either 0.98 or 1.0, whichever comes first. 
    3436 
    3537With this in mind, we recommend you use the following import statement when 
     
    185187    False 
    186188 
    187 Access the ``Form`` attribute ``errors`` to get a dictionary of error messages:: 
     189Access the ``errors`` attribute to get a dictionary of error messages:: 
    188190 
    189191    >>> f.errors 
     
    197199form's data will be validated the first time either you call ``is_valid()`` or 
    198200access ``errors``. 
     201 
     202The validation routines will only get called once, regardless of how many times 
     203you access ``errors`` or call ``is_valid()``. This means that if validation has 
     204side effects, those side effects will only be triggered once. 
    199205 
    200206Behavior of unbound forms 
     
    274280    >>> f.clean_data # Doesn't contain extra_field_1, etc. 
    275281    {'cc_myself': True, 'message': u'Hi there', 'sender': u'foo@example.com', 'subject': u'hello'} 
     282 
     283``clean_data`` will include a key and value for *all* fields defined in the 
     284``Form``, even if the data didn't include a value for fields that are not 
     285required. In this example, the data dictionary doesn't include a value for the 
     286``nick_name`` field, but ``clean_data`` includes it, with an empty value:: 
     287 
     288    >>> class OptionalPersonForm(Form): 
     289    ...     first_name = CharField() 
     290    ...     last_name = CharField() 
     291    ...     nick_name = CharField(required=False) 
     292    >>> data = {'first_name': u'John', 'last_name': u'Lennon'} 
     293    >>> f = OptionalPersonForm(data) 
     294    >>> f.is_valid() 
     295    True 
     296    >>> f.clean_data 
     297    {'nick_name': u'', 'first_name': u'John', 'last_name': u'Lennon'} 
     298 
     299In this above example, the ``clean_data`` value for ``nick_name`` is set to an 
     300empty string, because ``nick_name`` is ``CharField``, and ``CharField``\s treat 
     301empty values as an empty string. Each field type knows what its "blank" value 
     302is -- e.g., for ``DateField``, it's ``None`` instead of the empty string. 
    276303 
    277304Behavior of unbound forms 
     
    455482then the form output will include ``<label>`` tags, and will generate ``id`` 
    456483attributes based on the format string. For example, for a format string 
    457 ``'field_%s'``, a field named ``subject`` will get the ``id`` 
     484``'field_%s'``, a field named ``subject`` will get the ``id`` value 
    458485``'field_subject'``. Continuing our example:: 
    459486 
     
    494521If you render a bound ``Form`` object, the act of rendering will automatically 
    495522run the form's validation if it hasn't already happened, and the HTML output 
    496 will include the validation errors as a ``<ul>`` near the field. The particular 
    497 positioning of the error messages depends on the output method you're using:: 
     523will include the validation errors as a ``<ul class="errorlist">`` near the 
     524field. The particular positioning of the error messages depends on the output 
     525method you're using:: 
    498526 
    499527    >>> data = {'subject': '', 
     
    557585 
    558586For a field's list of errors, access the field's ``errors`` attribute. This 
    559 is a list-like object that is displayed as an HTML ``<ul>`` when printed:: 
     587is a list-like object that is displayed as an HTML ``<ul class="errorlist">`` 
     588when printed:: 
    560589 
    561590    >>> data = {'subject': 'hi', 'message': '', 'sender': '', 'cc_myself': ''} 
     
    647676Each ``Field`` class constructor takes at least these arguments. Some 
    648677``Field`` classes take additional, field-specific arguments, but the following 
    649 should *always* be available
     678should *always* be accepted
    650679 
    651680``required`` 
     
    705734field. This is used when the ``Field`` is displayed in a ``Form``. 
    706735 
    707 As explained in _`Outputting forms as HTML` above, the default label for a 
     736As explained in "Outputting forms as HTML" above, the default label for a 
    708737``Field`` is generated from the field name by converting all underscores to 
    709738spaces and upper-casing the first letter. Specify ``label`` if that default 
     
    780809 
    781810The ``widget`` argument lets you specify a ``Widget`` class to use when 
    782 rendering this ``Field``. See _`Widgets` below for more information. 
     811rendering this ``Field``. See "Widgets" below for more information. 
    783812 
    784813``help_text`` 
     
    787816The ``help_text`` argument lets you specify descriptive text for this 
    788817``Field``. If you provide ``help_text``, it will be displayed next to the 
    789 ``Field`` when the ``Field`` is rendered in a ``Form``. 
     818``Field`` when the ``Field`` is rendered by one of the convenience ``Form`` 
     819methods (e.g., ``as_ul()``). 
    790820 
    791821Here's a full example ``Form`` that implements ``help_text`` for two of its 
     
    860890    <tr><th>Url:</th><td><input type="text" name="url" /></td></tr> 
    861891    <tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr> 
     892 
     893Built-in ``Field`` classes 
     894-------------------------- 
     895 
     896Naturally, the ``newforms`` library comes with a set of ``Field`` classes that 
     897represent common validation needs. This section documents each built-in field. 
     898 
     899For each field, we describe the default widget used if you don't specify 
     900``widget``. We also specify the value returned when you provide an empty value 
     901(see the section on ``required`` above to understand what that means). 
     902 
     903``BooleanField`` 
     904~~~~~~~~~~~~~~~~ 
     905 
     906    * Default widget: ``CheckboxInput`` 
     907    * Empty value: ``None`` 
     908    * Normalizes to: A Python ``True`` or ``False`` value. 
     909    * Validates nothing (i.e., it never raises a ``ValidationError``). 
     910 
     911``CharField`` 
     912~~~~~~~~~~~~~ 
     913 
     914    * Default widget: ``TextInput`` 
     915    * Empty value: ``''`` (an empty string) 
     916    * Normalizes to: A Unicode object. 
     917    * Validates nothing, unless ``max_length`` or ``min_length`` is provided. 
     918 
     919Has two optional arguments for validation, ``max_length`` and ``min_length``. 
     920If provided, these arguments ensure that the string is at most or at least the 
     921given length. 
     922 
     923``ChoiceField`` 
     924~~~~~~~~~~~~~~~ 
     925 
     926    * Default widget: ``Select`` 
     927    * Empty value: ``''`` (an empty string) 
     928    * Normalizes to: A Unicode object. 
     929    * Validates that the given value exists in the list of choices. 
     930 
     931Takes one extra argument, ``choices``, which is an iterable (e.g., a list or 
     932tuple) of 2-tuples to use as choices for this field. 
     933 
     934``DateField`` 
     935~~~~~~~~~~~~~ 
     936 
     937    * Default widget: ``TextInput`` 
     938    * Empty value: ``None`` 
     939    * Normalizes to: A Python ``datetime.date`` object. 
     940    * Validates that the given value is either a ``datetime.date``, 
     941      ``datetime.datetime`` or string formatted in a particular date format. 
     942 
     943Takes one optional argument, ``input_formats``, which is a list of formats used 
     944to attempt to convert a string to a valid ``datetime.date`` object. 
     945 
     946If no ``input_formats`` argument is provided, the default input formats are:: 
     947 
     948    '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', # '2006-10-25', '10/25/2006', '10/25/06' 
     949    '%b %d %Y', '%b %d, %Y',            # 'Oct 25 2006', 'Oct 25, 2006' 
     950    '%d %b %Y', '%d %b, %Y',            # '25 Oct 2006', '25 Oct, 2006' 
     951    '%B %d %Y', '%B %d, %Y',            # 'October 25 2006', 'October 25, 2006' 
     952    '%d %B %Y', '%d %B, %Y',            # '25 October 2006', '25 October, 2006' 
     953 
     954``DateTimeField`` 
     955~~~~~~~~~~~~~~~~~ 
     956 
     957    * Default widget: ``TextInput`` 
     958    * Empty value: ``None`` 
     959    * Normalizes to: A Python ``datetime.datetime`` object. 
     960    * Validates that the given value is either a ``datetime.datetime``, 
     961      ``datetime.date`` or string formatted in a particular datetime format. 
     962 
     963Takes one optional argument, ``input_formats``, which is a list of formats used 
     964to attempt to convert a string to a valid ``datetime.datetime`` object. 
     965 
     966If no ``input_formats`` argument is provided, the default input formats are:: 
     967 
     968    '%Y-%m-%d %H:%M:%S',     # '2006-10-25 14:30:59' 
     969    '%Y-%m-%d %H:%M',        # '2006-10-25 14:30' 
     970    '%Y-%m-%d',              # '2006-10-25' 
     971    '%m/%d/%Y %H:%M:%S',     # '10/25/2006 14:30:59' 
     972    '%m/%d/%Y %H:%M',        # '10/25/2006 14:30' 
     973    '%m/%d/%Y',              # '10/25/2006' 
     974    '%m/%d/%y %H:%M:%S',     # '10/25/06 14:30:59' 
     975    '%m/%d/%y %H:%M',        # '10/25/06 14:30' 
     976    '%m/%d/%y',              # '10/25/06' 
     977 
     978``EmailField`` 
     979~~~~~~~~~~~~~~ 
     980 
     981    * Default widget: ``TextInput`` 
     982    * Empty value: ``''`` (an empty string) 
     983    * Normalizes to: A Unicode object. 
     984    * Validates that the given value is a valid e-mail address, using a 
     985      moderately complex regular expression. 
     986 
     987Has two optional arguments for validation, ``max_length`` and ``min_length``. 
     988If provided, these arguments ensure that the string is at most or at least the 
     989given length. 
     990 
     991``IntegerField`` 
     992~~~~~~~~~~~~~~~~ 
     993 
     994    * Default widget: ``TextInput`` 
     995    * Empty value: ``None`` 
     996    * Normalizes to: A Python integer or long integer. 
     997    * Validates that the given value is an integer. Leading and trailing 
     998      whitespace is allowed, as in Python's ``int()`` function. 
     999 
     1000``MultipleChoiceField`` 
     1001~~~~~~~~~~~~~~~~~~~~~~~ 
     1002 
     1003    * Default widget: ``SelectMultiple`` 
     1004    * Empty value: ``[]`` (an empty list) 
     1005    * Normalizes to: A list of Unicode objects. 
     1006    * Validates that every value in the given list of values exists in the list 
     1007      of choices. 
     1008 
     1009Takes one extra argument, ``choices``, which is an iterable (e.g., a list or 
     1010tuple) of 2-tuples to use as choices for this field. 
     1011 
     1012``NullBooleanField`` 
     1013~~~~~~~~~~~~~~~~~~~~ 
     1014 
     1015    * Default widget: ``NullBooleanSelect`` 
     1016    * Empty value: ``None`` 
     1017    * Normalizes to: A Python ``True``, ``False`` or ``None`` value. 
     1018    * Validates nothing (i.e., it never raises a ``ValidationError``). 
     1019 
     1020``RegexField`` 
     1021~~~~~~~~~~~~~~ 
     1022 
     1023    * Default widget: ``TextInput`` 
     1024    * Empty value: ``''`` (an empty string) 
     1025    * Normalizes to: A Unicode object. 
     1026    * Validates that the given value matches against a certain regular 
     1027      expression. 
     1028 
     1029Takes one required argument, ``regex``, which is a regular expression specified 
     1030either as a string or a compiled regular expression object. 
     1031 
     1032Also takes the following optional arguments: 
     1033 
     1034    ======================  ===================================================== 
     1035    Argument                Description 
     1036    ======================  ===================================================== 
     1037    ``max_length``          Ensures the string has at most this many characters. 
     1038    ``min_length``          Ensures the string has at least this many characters. 
     1039    ``error_message``       Error message to return for failed validation. If no 
     1040                            message is provided, a generic error message will be 
     1041                            used. 
     1042    ======================  ===================================================== 
     1043 
     1044``TimeField`` 
     1045~~~~~~~~~~~~~ 
     1046 
     1047    * Default widget: ``TextInput`` 
     1048    * Empty value: ``None`` 
     1049    * Normalizes to: A Python ``datetime.time`` object. 
     1050    * Validates that the given value is either a ``datetime.time`` or string 
     1051      formatted in a particular time format. 
     1052 
     1053Takes one optional argument, ``input_formats``, which is a list of formats used 
     1054to attempt to convert a string to a valid ``datetime.time`` object. 
     1055 
     1056If no ``input_formats`` argument is provided, the default input formats are:: 
     1057 
     1058    '%H:%M:%S',     # '14:30:59' 
     1059    '%H:%M',        # '14:30' 
     1060 
     1061``URLField`` 
     1062~~~~~~~~~~~~ 
     1063 
     1064    * Default widget: ``TextInput`` 
     1065    * Empty value: ``''`` (an empty string) 
     1066    * Normalizes to: A Unicode object. 
     1067    * Validates that the given value is a valid URL. 
     1068 
     1069Takes the following optional arguments: 
     1070 
     1071    ========================  ===================================================== 
     1072    Argument                  Description 
     1073    ========================  ===================================================== 
     1074    ``max_length``            Ensures the string has at most this many characters. 
     1075    ``min_length``            Ensures the string has at least this many characters. 
     1076    ``verify_exists``         If ``True``, the validator will attempt to load the 
     1077                              given URL, raising ``ValidationError`` if the page 
     1078                              gives a 404. Defaults to ``False``. 
     1079    ``validator_user_agent``  String used as the user-agent used when checking for 
     1080                              a URL's existence. Defaults to the value of the 
     1081                              ``URL_VALIDATOR_USER_AGENT`` setting. 
     1082    ========================  ===================================================== 
     1083 
     1084Slightly complex built-in ``Field`` classes 
     1085------------------------------------------- 
     1086 
     1087The following are not yet documented here. See the unit tests, linked-to from 
     1088the bottom of this document, for examples of their use. 
     1089 
     1090``ComboField`` 
     1091~~~~~~~~~~~~~~ 
     1092 
     1093``MultiValueField`` 
     1094~~~~~~~~~~~~~~~~~~~ 
     1095 
     1096``SplitDateTimeField`` 
     1097~~~~~~~~~~~~~~~~~~~~~~ 
    8621098 
    8631099Creating custom fields 
  • django/branches/unicode/docs/testing.txt

    r5214 r5224  
    504504 
    505505E-mail services 
    506 -------------- 
     506--------------- 
    507507 
    508508**New in Django development version** 
  • django/branches/unicode/tests/regressiontests/forms/tests.py

    r5214 r5224  
    19161916>>> p.clean_data 
    19171917{'first_name': u'John', 'last_name': u'Lennon', 'birthday': datetime.date(1940, 10, 9)} 
     1918 
     1919clean_data will include a key and value for *all* fields defined in the Form, 
     1920even if the Form's data didn't include a value for fields that are not 
     1921required. In this example, the data dictionary doesn't include a value for the 
     1922"nick_name" field, but clean_data includes it. For CharFields, it's set to the 
     1923empty string. 
     1924>>> class OptionalPersonForm(Form): 
     1925...     first_name = CharField() 
     1926...     last_name = CharField() 
     1927...     nick_name = CharField(required=False) 
     1928>>> data = {'first_name': u'John', 'last_name': u'Lennon'} 
     1929>>> f = OptionalPersonForm(data) 
     1930>>> f.is_valid() 
     1931True 
     1932>>> f.clean_data 
     1933{'nick_name': u'', 'first_name': u'John', 'last_name': u'Lennon'} 
     1934 
     1935For DateFields, it's set to None. 
     1936>>> class OptionalPersonForm(Form): 
     1937...     first_name = CharField() 
     1938...     last_name = CharField() 
     1939...     birth_date = DateField(required=False) 
     1940>>> data = {'first_name': u'John', 'last_name': u'Lennon'} 
     1941>>> f = OptionalPersonForm(data) 
     1942>>> f.is_valid() 
     1943True 
     1944>>> f.clean_data 
     1945{'birth_date': None, 'first_name': u'John', 'last_name': u'Lennon'} 
    19181946 
    19191947"auto_id" tells the Form to add an "id" attribute to each form element. 
     
    33793407 
    33803408# MultiWidget and MultiValueField ############################################# 
    3381 # MultiWidgets are widgets composed of other widgets. They are usually  
     3409# MultiWidgets are widgets composed of other widgets. They are usually 
    33823410# combined with MultiValueFields - a field that is composed of other fields. 
    33833411# MulitWidgets can themselved be composed of other MultiWidgets. 
     
    33873415...     def __init__(self, attrs=None): 
    33883416...         widgets = ( 
    3389 ...             TextInput(),  
     3417...             TextInput(), 
    33903418...             SelectMultiple(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), 
    33913419...             SplitDateTimeWidget(), 
     
    34123440 
    34133441>>> class ComplexField(MultiValueField): 
    3414 ...     def __init__(self, required=True, widget=None, label=None, initial=None):  
     3442...     def __init__(self, required=True, widget=None, label=None, initial=None): 
    34153443...         fields = ( 
    3416 ...             CharField(),  
     3444...             CharField(), 
    34173445...             MultipleChoiceField(choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), 
    34183446...             SplitDateTimeField() 
    34193447...         ) 
    3420 ...         super(ComplexField, self).__init__(fields, required, widget, label, initial)  
     3448...         super(ComplexField, self).__init__(fields, required, widget, label, initial) 
    34213449... 
    34223450...     def compress(self, data_list):