Django

Code

Changeset 4441

Show
Ignore:
Timestamp:
01/28/07 16:26:25 (2 years ago)
Author:
adrian
Message:

newforms-admin: Merged to [4440]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/newforms-admin/django/newforms/fields.py

    r4412 r4441  
    3636    creation_counter = 0 
    3737 
    38     def __init__(self, required=True, widget=None, label=None, initial=None): 
     38    def __init__(self, required=True, widget=None, label=None, initial=None, help_text=None): 
    3939        # required -- Boolean that specifies whether the field is required. 
    4040        #             True by default. 
     
    4848        # initial -- A value to use in this Field's initial display. This value is 
    4949        #            *not* used as a fallback if data isn't given. 
     50        # help_text -- An optional string to use as "help text" for this Field. 
    5051        if label is not None: 
    5152            label = smart_unicode(label) 
    5253        self.required, self.label, self.initial = required, label, initial 
     54        self.help_text = help_text 
    5355        widget = widget or self.widget 
    5456        if isinstance(widget, type): 
     
    8688 
    8789class CharField(Field): 
    88     def __init__(self, max_length=None, min_length=None, required=True, widget=None, label=None, initial=None): 
     90    def __init__(self, max_length=None, min_length=None, *args, **kwargs): 
    8991        self.max_length, self.min_length = max_length, min_length 
    90         super(CharField, self).__init__(required, widget, label, initial
     92        super(CharField, self).__init__(*args, **kwargs
    9193 
    9294    def clean(self, value): 
     
    107109 
    108110class IntegerField(Field): 
    109     def __init__(self, max_value=None, min_value=None, required=True, widget=None, label=None, initial=None): 
     111    def __init__(self, max_value=None, min_value=None, *args, **kwargs): 
    110112        self.max_value, self.min_value = max_value, min_value 
    111         super(IntegerField, self).__init__(required, widget, label, initial
     113        super(IntegerField, self).__init__(*args, **kwargs
    112114 
    113115    def clean(self, value): 
     
    138140 
    139141class DateField(Field): 
    140     def __init__(self, input_formats=None, required=True, widget=None, label=None, initial=None): 
    141         super(DateField, self).__init__(required, widget, label, initial
     142    def __init__(self, input_formats=None, *args, **kwargs): 
     143        super(DateField, self).__init__(*args, **kwargs
    142144        self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS 
    143145 
     
    167169 
    168170class TimeField(Field): 
    169     def __init__(self, input_formats=None, required=True, widget=None, label=None, initial=None): 
    170         super(TimeField, self).__init__(required, widget, label, initial
     171    def __init__(self, input_formats=None, *args, **kwargs): 
     172        super(TimeField, self).__init__(*args, **kwargs
    171173        self.input_formats = input_formats or DEFAULT_TIME_INPUT_FORMATS 
    172174 
     
    201203 
    202204class DateTimeField(Field): 
    203     def __init__(self, input_formats=None, required=True, widget=None, label=None, initial=None): 
    204         super(DateTimeField, self).__init__(required, widget, label, initial
     205    def __init__(self, input_formats=None, *args, **kwargs): 
     206        super(DateTimeField, self).__init__(*args, **kwargs
    205207        self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS 
    206208 
     
    225227 
    226228class RegexField(Field): 
    227     def __init__(self, regex, max_length=None, min_length=None, error_message=None, 
    228             required=True, widget=None, label=None, initial=None): 
     229    def __init__(self, regex, max_length=None, min_length=None, error_message=None, *args, **kwargs): 
    229230        """ 
    230231        regex can be either a string or a compiled regular expression object. 
     
    232233        'Enter a valid value' is too generic for you. 
    233234        """ 
    234         super(RegexField, self).__init__(required, widget, label, initial
     235        super(RegexField, self).__init__(*args, **kwargs
    235236        if isinstance(regex, basestring): 
    236237            regex = re.compile(regex) 
     
    264265 
    265266class EmailField(RegexField): 
    266     def __init__(self, max_length=None, min_length=None, required=True, widget=None, label=None, initial=None): 
    267         RegexField.__init__(self, email_re, max_length, min_length, gettext(u'Enter a valid e-mail address.'), required, widget, label, initial) 
     267    def __init__(self, max_length=None, min_length=None, *args, **kwargs): 
     268        RegexField.__init__(self, email_re, max_length, min_length, 
     269            gettext(u'Enter a valid e-mail address.'), *args, **kwargs) 
    268270 
    269271url_re = re.compile( 
     
    281283 
    282284class URLField(RegexField): 
    283     def __init__(self, max_length=None, min_length=None, required=True, verify_exists=False, widget=None, label=None, 
    284             initial=None, validator_user_agent=URL_VALIDATOR_USER_AGENT): 
    285         super(URLField, self).__init__(url_re, max_length, min_length, gettext(u'Enter a valid URL.'), required, widget, label, initial
     285    def __init__(self, max_length=None, min_length=None, verify_exists=False, 
     286            validator_user_agent=URL_VALIDATOR_USER_AGENT, *args, **kwargs): 
     287        super(URLField, self).__init__(url_re, max_length, min_length, gettext(u'Enter a valid URL.'), *args, **kwargs
    286288        self.verify_exists = verify_exists 
    287289        self.user_agent = validator_user_agent 
     
    329331 
    330332class ChoiceField(Field): 
    331     def __init__(self, choices=(), required=True, widget=Select, label=None, initial=None): 
    332         if isinstance(widget, type): 
    333             widget = widget() 
    334         super(ChoiceField, self).__init__(required, widget, label, initial) 
     333    def __init__(self, choices=(), required=True, widget=Select, label=None, initial=None, help_text=None): 
     334        super(ChoiceField, self).__init__(required, widget, label, initial, help_text) 
    335335        self.choices = choices 
    336336 
     
    363363    hidden_widget = MultipleHiddenInput 
    364364 
    365     def __init__(self, choices=(), required=True, widget=SelectMultiple, label=None, initial=None): 
    366         super(MultipleChoiceField, self).__init__(choices, required, widget, label, initial
     365    def __init__(self, choices=(), required=True, widget=SelectMultiple, label=None, initial=None, help_text=None): 
     366        super(MultipleChoiceField, self).__init__(choices, required, widget, label, initial, help_text
    367367 
    368368    def clean(self, value): 
     
    391391    A Field whose clean() method calls multiple Field clean() methods. 
    392392    """ 
    393     def __init__(self, fields=(), required=True, widget=None, label=None, initial=None): 
    394         super(ComboField, self).__init__(required, widget, label, initial
     393    def __init__(self, fields=(), *args, **kwargs): 
     394        super(ComboField, self).__init__(*args, **kwargs
    395395        # Set 'required' to False on the individual fields, because the 
    396396        # required validation will be handled by ComboField, not by those 
     
    426426    You'll probably want to use this with MultiWidget. 
    427427    """ 
    428     def __init__(self, fields=(), required=True, widget=None, label=None, initial=None): 
    429         super(MultiValueField, self).__init__(required, widget, label, initial
     428    def __init__(self, fields=(), *args, **kwargs): 
     429        super(MultiValueField, self).__init__(*args, **kwargs
    430430        # Set 'required' to False on the individual fields, because the 
    431431        # required validation will be handled by MultiValueField, not by those 
     
    482482 
    483483class SplitDateTimeField(MultiValueField): 
    484     def __init__(self, required=True, widget=None, label=None, initial=None): 
     484    def __init__(self, *args, **kwargs): 
    485485        fields = (DateField(), TimeField()) 
    486         super(SplitDateTimeField, self).__init__(fields, required, widget, label, initial
     486        super(SplitDateTimeField, self).__init__(fields, *args, **kwargs
    487487 
    488488    def compress(self, data_list): 
  • django/branches/newforms-admin/django/newforms/forms.py

    r4371 r4441  
    2727        dict.__init__(self, dict(data)) 
    2828 
     29    def copy(self): 
     30        return SortedDictFromList(self.items()) 
     31 
    2932class DeclarativeFieldsMetaclass(type): 
    30     "Metaclass that converts Field attributes to a dictionary called 'fields'." 
     33    "Metaclass that converts Field attributes to a dictionary called 'base_fields'." 
    3134    def __new__(cls, name, bases, attrs): 
    3235        fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)] 
    3336        fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter)) 
    34         attrs['fields'] = SortedDictFromList(fields) 
     37        attrs['base_fields'] = SortedDictFromList(fields) 
    3538        return type.__new__(cls, name, bases, attrs) 
    3639 
     
    4750        self.initial = initial or {} 
    4851        self.__errors = None # Stores the errors after clean() has been called. 
     52        # The base_fields class attribute is the *class-wide* definition of 
     53        # fields. Because a particular *instance* of the class might want to 
     54        # alter self.fields, we create self.fields here by copying base_fields. 
     55        # Instances should always modify self.fields; they should not modify 
     56        # self.base_fields. 
     57        self.fields = self.base_fields.copy() 
    4958 
    5059    def __unicode__(self): 
     
    8695        return self.prefix and ('%s-%s' % (self.prefix, field_name)) or field_name 
    8796 
    88     def _html_output(self, normal_row, error_row, row_ender, errors_on_separate_row): 
     97    def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row): 
    8998        "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()." 
    9099        top_errors = self.non_field_errors() # Errors that should be displayed above all fields. 
     
    101110                    output.append(error_row % bf_errors) 
    102111                label = bf.label and bf.label_tag(escape(bf.label + ':')) or '' 
    103                 output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf)}) 
     112                if field.help_text: 
     113                    help_text = help_text_html % field.help_text 
     114                else: 
     115                    help_text = u'' 
     116                output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text}) 
    104117        if top_errors: 
    105118            output.insert(0, error_row % top_errors) 
     
    116129    def as_table(self): 
    117130        "Returns this form rendered as HTML <tr>s -- excluding the <table></table>." 
    118         return self._html_output(u'<tr><th>%(label)s</th><td>%(errors)s%(field)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', False) 
     131        return self._html_output(u'<tr><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False) 
    119132 
    120133    def as_ul(self): 
    121134        "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>." 
    122         return self._html_output(u'<li>%(errors)s%(label)s %(field)s</li>', u'<li>%s</li>', '</li>', False) 
     135        return self._html_output(u'<li>%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False) 
    123136 
    124137    def as_p(self): 
    125138        "Returns this form rendered as HTML <p>s." 
    126         return self._html_output(u'<p>%(label)s %(field)s</p>', u'<p>%s</p>', '</p>', True) 
     139        return self._html_output(u'<p>%(label)s %(field)s%(help_text)s</p>', u'<p>%s</p>', '</p>', u' %s', True) 
    127140 
    128141    def non_field_errors(self): 
  • django/branches/newforms-admin/django/newforms/models.py

    r4388 r4441  
    1616    if self.errors: 
    1717        raise ValueError("The %s could not be created because the data didn't validate." % self._model._meta.object_name) 
    18     obj = self._model(**self.clean_data) 
    19     if commit: 
    20         obj.save() 
    21     return obj 
     18    return save_instance(self, self._model(), commit) 
    2219 
    2320def save_instance(form, instance, commit=True): 
     
    3431        raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) 
    3532    clean_data = form.clean_data 
    36     for f in opts.fields + opts.many_to_many
     33    for f in opts.fields
    3734        if isinstance(f, models.AutoField): 
    3835            continue 
     
    4037    if commit: 
    4138        instance.save() 
     39        for f in opts.many_to_many: 
     40            setattr(instance, f.attname, getattr(instance, f.attname).model.objects.filter(pk__in = clean_data[f.name])) 
     41    # GOTCHA: If many-to-many data is given and commit=False, the many-to-many 
     42    # data will be lost. This happens because a many-to-many options cannot be 
     43    # set on an object until after it's saved. Maybe we should raise an 
     44    # exception in that case. 
    4245    return instance 
    4346 
     
    6568            field_list.append((f.name, formfield)) 
    6669    fields = SortedDictFromList(field_list) 
    67     return type(opts.object_name + 'Form', (form,), {'fields': fields, '_model': model, 'save': model_save}) 
     70    return type(opts.object_name + 'Form', (form,), {'base_fields': fields, '_model': model, 'save': model_save}) 
    6871 
    6972def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): 
     
    8891    fields = SortedDictFromList(field_list) 
    8992    return type(opts.object_name + 'InstanceForm', (form,), 
    90         {'fields': fields, '_model': model, 'save': make_instance_save(instance)}) 
     93        {'base_fields': fields, '_model': model, 'save': make_instance_save(instance)}) 
    9194 
    9295def form_for_fields(field_list): 
    9396    "Returns a Form class for the given list of Django database field instances." 
    9497    fields = SortedDictFromList([(f.name, f.formfield()) for f in field_list]) 
    95     return type('FormForFields', (BaseForm,), {'fields': fields}) 
     98    return type('FormForFields', (BaseForm,), {'base_fields': fields}) 
  • django/branches/newforms-admin/django/newforms/widgets.py

    r4412 r4441  
    137137class Select(Widget): 
    138138    def __init__(self, attrs=None, choices=()): 
    139         # choices can be any iterable 
    140         self.attrs = attrs or {} 
    141         self.choices = choices 
     139        self.attrs = attrs or {} 
     140        # choices can be any iterable, but we may need to render this widget 
     141        # multiple times. Thus, collapse it into a list so it can be consumed 
     142        # more than once. 
     143        self.choices = list(choices) 
    142144 
    143145    def render(self, name, value, attrs=None, choices=()): 
     
    257259    def render(self, name, value, attrs=None, choices=()): 
    258260        if value is None: value = [] 
     261        has_id = attrs and attrs.has_key('id') 
    259262        final_attrs = self.build_attrs(attrs, name=name) 
    260263        output = [u'<ul>'] 
    261264        str_values = set([smart_unicode(v) for v in value]) # Normalize to strings. 
    262         cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) 
    263         for option_value, option_label in chain(self.choices, choices): 
     265        for i, (option_value, option_label) in enumerate(chain(self.choices, choices)): 
     266            # If an ID attribute was given, add a numeric index as a suffix, 
     267            # so that the checkboxes don't all have the same ID attribute. 
     268            if has_id: 
     269                final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i)) 
     270            cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) 
    264271            option_value = smart_unicode(option_value) 
    265272            rendered_cb = cb.render(name, option_value) 
  • django/branches/newforms-admin/docs/newforms.txt

    r4410 r4441  
    740740rendering this ``Field``. See _`Widgets` below for more information. 
    741741 
     742``help_text`` 
     743~~~~~~~~~~~~~ 
     744 
     745The ``help_text`` argument lets you specify descriptive text for this 
     746``Field``. If you provide ``help_text``, it will be displayed next to the 
     747``Field`` when the ``Field`` is rendered in a ``Form``. 
     748 
     749Here's a full example ``Form`` that implements ``help_text`` for two of its 
     750fields. We've specified ``auto_id=False`` to simplify the output:: 
     751 
     752    >>> class HelpTextContactForm(forms.Form): 
     753    ...     subject = forms.CharField(max_length=100, help_text='100 characters max.') 
     754    ...     message = forms.CharField() 
     755    ...     sender = forms.EmailField(help_text='A valid e-mail address, please.') 
     756    ...     cc_myself = forms.BooleanField() 
     757    >>> f = HelpTextContactForm(auto_id=False) 
     758    >>> print f.as_table() 
     759    <tr><th>Subject:</th><td><input type="text" name="subject" maxlength="100" /><br />100 characters max.</td></tr> 
     760    <tr><th>Message:</th><td><input type="text" name="message" /></td></tr> 
     761    <tr><th>Sender:</th><td><input type="text" name="sender" /><br />A valid e-mail address, please.</td></tr> 
     762    <tr><th>Cc myself:</th><td><input type="checkbox" name="cc_myself" /></td></tr> 
     763    >>> print f.as_ul() 
     764    <li>Subject: <input type="text" name="subject" maxlength="100" /> 100 characters max.</li> 
     765    <li>Message: <input type="text" name="message" /></li> 
     766    <li>Sender: <input type="text" name="sender" /> A valid e-mail address, please.</li> 
     767    <li>Cc myself: <input type="checkbox" name="cc_myself" /></li> 
     768    >>> print f.as_p() 
     769    <p>Subject: <input type="text" name="subject" maxlength="100" /> 100 characters max.</p> 
     770    <p>Message: <input type="text" name="message" /></p> 
     771    <p>Sender: <input type="text" name="sender" /> A valid e-mail address, please.</p> 
     772    <p>Cc myself: <input type="checkbox" name="cc_myself" /></p> 
     773 
    742774Dynamic initial values 
    743775---------------------- 
  • django/branches/newforms-admin/tests/modeltests/model_forms/models.py

    r4305 r4441  
    225225</select></li> 
    226226 
     227>>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04', 
     228...     'writer': u'1', 'article': u'Hello.', 'categories': [u'1', u'2']}) 
     229>>> new_art = f.save() 
     230>>> new_art.id 
     2311 
     232>>> new_art = Article.objects.get(id=1) 
     233>>> new_art.categories.all() 
     234[<Category: Entertainment>, <Category: It's a test>] 
     235 
     236Now, submit form data with no categories. This deletes the existing categories. 
     237>>> f = TestArticleForm({'headline': u'New headline', 'pub_date': u'1988-01-04', 
     238...     'writer': u'1', 'article': u'Hello.'}) 
     239>>> new_art = f.save() 
     240>>> new_art.id 
     2411 
     242>>> new_art = Article.objects.get(id=1) 
     243>>> new_art.categories.all() 
     244[] 
     245 
     246Create a new article, with categories, via the form. 
     247>>> ArticleForm = form_for_model(Article) 
     248>>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01', 
     249...     'writer': u'1', 'article': u'Test.', 'categories': [u'1', u'2']}) 
     250>>> new_art = f.save() 
     251>>> new_art.id 
     2522 
     253>>> new_art = Article.objects.get(id=2) 
     254>>> new_art.categories.all() 
     255[<Category: Entertainment>, <Category: It's a test>] 
     256 
     257Create a new article, with no categories, via the form. 
     258>>> ArticleForm = form_for_model(Article) 
     259>>> f = ArticleForm({'headline': u'The walrus was Paul', 'pub_date': u'1967-11-01', 
     260...     'writer': u'1', 'article': u'Test.'}) 
     261>>> new_art = f.save() 
     262>>> new_art.id 
     2633 
     264>>> new_art = Article.objects.get(id=3) 
     265>>> new_art.categories.all() 
     266[] 
     267 
    227268Here, we define a custom Form. Because it happens to have the same fields as 
    228269the Category model, we can use save_instance() to apply its changes to an 
  • django/branches/newforms-admin/tests/regressiontests/forms/tests.py

    r4413 r4441  
    336336>>> w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) 
    337337u'<select name="email">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected">\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</option>\n<option value="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</option>\n</select>' 
     338 
     339If choices is passed to the constructor and is a generator, it can be iterated 
     340over multiple times without getting consumed: 
     341>>> w = Select(choices=get_choices()) 
     342>>> print w.render('num', 2) 
     343<select name="num"> 
     344<option value="0">0</option> 
     345<option value="1">1</option> 
     346<option value="2" selected="selected">2</option> 
     347<option value="3">3</option> 
     348<option value="4">4</option> 
     349</select> 
     350>>> print w.render('num', 3) 
     351<select name="num"> 
     352<option value="0">0</option> 
     353<option value="1">1</option> 
     354<option value="2">2</option> 
     355<option value="3" selected="selected">3</option> 
     356<option value="4">4</option> 
     357</select> 
    338358 
    339359# NullBooleanSelect Widget #################################################### 
     
    21282148<li><label><input checked="checked" type="checkbox" name="composers" value="J" /> John Lennon</label></li> 
    21292149<li><label><input checked="checked" type="checkbox" name="composers" value="P" /> Paul McCartney</label></li> 
     2150</ul> 
     2151 
     2152Regarding auto_id, CheckboxSelectMultiple is a special case. Each checkbox 
     2153gets a distinct ID, formed by appending an underscore plus the checkbox's 
     2154zero-based index. 
     2155>>> f = SongForm(auto_id='%s_id') 
     2156>>> print f['composers'] 
     2157<ul> 
     2158<li><label><input type="checkbox" name="composers" value="J" id="composers_id_0" /> John Lennon</label></li> 
     2159<li><label><input type="checkbox" name="composers" value="P" id="composers_id_1" /> Paul McCartney</label></li> 
    21302160</ul> 
    21312161 
     
    22482278{'username': u'adrian', 'password1': u'foo', 'password2': u'foo'} 
    22492279 
     2280# Dynamic construction ######################################################## 
     2281 
    22502282It's possible to construct a Form dynamically by adding to the self.fields 
    22512283dictionary in __init__(). Don't forget to call Form.__init__() within the 
     
    22622294<tr><th>Last name:</th><td><input type="text" name="last_name" /></td></tr> 
    22632295<tr><th>Birthday:</th><td><input type="text" name="birthday" /></td></tr> 
     2296 
     2297Instances of a dynamic Form do not persist fields from one Form instance to 
     2298the next. 
     2299>>> class MyForm(Form): 
     2300...     def __init__(self, data=None, auto_id=False, field_list=[]): 
     2301...         Form.__init__(self, data, auto_id) 
     2302...         for field in field_list: 
     2303...             self.fields[field[0]] = field[1] 
     2304>>> field_list = [('field1', CharField()), ('field2', CharField())] 
     2305>>> my_form = MyForm(field_list=field_list) 
     2306>>> print my_form 
     2307<tr><th>Field1:</th><td><input type="text" name="field1" /></td></tr> 
     2308<tr><th>Field2:</th><td><input type="text" name="field2" /></td></tr> 
     2309>>> field_list = [('field3', CharField()), ('field4', CharField())] 
     2310>>> my_form = MyForm(field_list=field_list) 
     2311>>> print my_form 
     2312<tr><th>Field3:</th><td><input type="text" name="field3" /></td></tr> 
     2313<tr><th>Field4:</th><td><input type="text" name="field4" /></td></tr> 
     2314 
     2315>>> class MyForm(Form): 
     2316...     default_field_1 = CharField() 
     2317...     default_field_2 = CharField() 
     2318...     def __init__(self, data=None, auto_id=False, field_list=[]): 
     2319...         Form.__init__(self, data, auto_id) 
     2320...         for field in field_list: 
     2321...             self.fields[field[0]] = field[1] 
     2322>>> field_list = [('field1', CharField()), ('field2', CharField())] 
     2323>>> my_form = MyForm(field_list=field_list) 
     2324>>> print my_form 
     2325<tr><th>Default field 1:</th><td><input type="text" name="default_field_1" /></td></tr> 
     2326<tr><th>Default field 2:</th><td><input type="text" name="default_field_2" /></td></tr> 
     2327<tr><th>Field1:</th><td><input type="text" name="field1" /></td></tr> 
     2328<tr><th>Field2:</th><td><input type="text" name="field2" /></td></tr> 
     2329>>> field_list = [('field3', CharField()), ('field4', CharField())] 
     2330>>> my_form = MyForm(field_list=field_list) 
     2331>>> print my_form 
     2332<tr><th>Default field 1:</th><td><input type="text" name="default_field_1" /></td></tr> 
     2333<tr><th>Default field 2:</th><td><input type="text" name="default_field_2" /></td></tr> 
     2334<tr><th>Field3:</th><td><input type="text" name="field3" /></td></tr> 
     2335<tr><th>Field4:</th><td><input type="text" name="field4" /></td></tr> 
    22642336 
    22652337HiddenInput widgets are displayed differently in the as_table(), as_ul() 
     
    25392611<li>Password: <input type="password" name="password" /></li> 
    25402612 
     2613# Help text ################################################################### 
     2614 
     2615You can specify descriptive text for a field by using the 'help_text' argument 
     2616to a Field class. This help text is displayed when a Form is rendered. 
     2617>>> class UserRegistration(Form): 
     2618...    username = CharField(max_length=10, help_text='e.g., user@example.com') 
     2619...    password = CharField(widget=PasswordInput, help_text='Choose wisely.') 
     2620>>> p = UserRegistration(auto_id=False) 
     2621>>> print p.as_ul() 
     2622<li>Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li> 
     2623<li>Password: <input type="password" name="password" /> Choose wisely.</li> 
     2624>>> print p.as_p() 
     2625<p>Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</p> 
     2626<p>Password: <input type="password" name="password" /> Choose wisely.</p> 
     2627>>> print p.as_table() 
     2628<tr><th>Username:</th><td><input type="text" name="username" maxlength="10" /><br />e.g., user@example.com</td></tr> 
     2629<tr><th>Password:</th><td><input type="password" name="password" /><br />Choose wisely.</td></tr> 
     2630 
     2631The help text is displayed whether or not data is provided for the form. 
     2632>>> p = UserRegistration({'username': u'foo'}, auto_id=False) 
     2633>>> print p.as_ul() 
     2634<li>Username: <input type="text" name="username" value="foo" maxlength="10" /> e.g., user@example.com</li> 
     2635<li><ul class="errorlist"><li>This field is required.</li></ul>Password: <input type="password" name="password" /> Choose wisely.</li> 
     2636 
     2637help_text is not displayed for hidden fields. It can be used for documentation 
     2638purposes, though. 
     2639>>> class UserRegistration(Form): 
     2640...    username = CharField(max_length=10, help_text='e.g., user@example.com') 
     2641...    password = CharField(widget=PasswordInput) 
     2642...    next = CharField(widget=HiddenInput, initial='/', help_text='Redirect destination') 
     2643>>> p = UserRegistration(auto_id=False) 
     2644>>> print p.as_ul() 
     2645<li>Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li> 
     2646<li>Password: <input type="password" name="password" /><input type="hidden" name="next" value="/" /></li> 
     2647 
    25412648# Forms with prefixes ######################################################### 
    25422649