Changeset 4441
- Timestamp:
- 01/28/07 16:26:25 (2 years ago)
- Files:
-
- django/branches/newforms-admin/django/newforms/fields.py (modified) (16 diffs)
- django/branches/newforms-admin/django/newforms/forms.py (modified) (5 diffs)
- django/branches/newforms-admin/django/newforms/models.py (modified) (5 diffs)
- django/branches/newforms-admin/django/newforms/widgets.py (modified) (2 diffs)
- django/branches/newforms-admin/docs/newforms.txt (modified) (1 diff)
- django/branches/newforms-admin/tests/modeltests/model_forms/models.py (modified) (1 diff)
- django/branches/newforms-admin/tests/regressiontests/forms/tests.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/newforms-admin/django/newforms/fields.py
r4412 r4441 36 36 creation_counter = 0 37 37 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): 39 39 # required -- Boolean that specifies whether the field is required. 40 40 # True by default. … … 48 48 # initial -- A value to use in this Field's initial display. This value is 49 49 # *not* used as a fallback if data isn't given. 50 # help_text -- An optional string to use as "help text" for this Field. 50 51 if label is not None: 51 52 label = smart_unicode(label) 52 53 self.required, self.label, self.initial = required, label, initial 54 self.help_text = help_text 53 55 widget = widget or self.widget 54 56 if isinstance(widget, type): … … 86 88 87 89 class 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): 89 91 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) 91 93 92 94 def clean(self, value): … … 107 109 108 110 class 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): 110 112 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) 112 114 113 115 def clean(self, value): … … 138 140 139 141 class 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) 142 144 self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS 143 145 … … 167 169 168 170 class 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) 171 173 self.input_formats = input_formats or DEFAULT_TIME_INPUT_FORMATS 172 174 … … 201 203 202 204 class 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) 205 207 self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS 206 208 … … 225 227 226 228 class 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): 229 230 """ 230 231 regex can be either a string or a compiled regular expression object. … … 232 233 'Enter a valid value' is too generic for you. 233 234 """ 234 super(RegexField, self).__init__( required, widget, label, initial)235 super(RegexField, self).__init__(*args, **kwargs) 235 236 if isinstance(regex, basestring): 236 237 regex = re.compile(regex) … … 264 265 265 266 class 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) 268 270 269 271 url_re = re.compile( … … 281 283 282 284 class 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) 286 288 self.verify_exists = verify_exists 287 289 self.user_agent = validator_user_agent … … 329 331 330 332 class 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) 335 335 self.choices = choices 336 336 … … 363 363 hidden_widget = MultipleHiddenInput 364 364 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) 367 367 368 368 def clean(self, value): … … 391 391 A Field whose clean() method calls multiple Field clean() methods. 392 392 """ 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) 395 395 # Set 'required' to False on the individual fields, because the 396 396 # required validation will be handled by ComboField, not by those … … 426 426 You'll probably want to use this with MultiWidget. 427 427 """ 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) 430 430 # Set 'required' to False on the individual fields, because the 431 431 # required validation will be handled by MultiValueField, not by those … … 482 482 483 483 class SplitDateTimeField(MultiValueField): 484 def __init__(self, required=True, widget=None, label=None, initial=None):484 def __init__(self, *args, **kwargs): 485 485 fields = (DateField(), TimeField()) 486 super(SplitDateTimeField, self).__init__(fields, required, widget, label, initial)486 super(SplitDateTimeField, self).__init__(fields, *args, **kwargs) 487 487 488 488 def compress(self, data_list): django/branches/newforms-admin/django/newforms/forms.py
r4371 r4441 27 27 dict.__init__(self, dict(data)) 28 28 29 def copy(self): 30 return SortedDictFromList(self.items()) 31 29 32 class 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'." 31 34 def __new__(cls, name, bases, attrs): 32 35 fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)] 33 36 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) 35 38 return type.__new__(cls, name, bases, attrs) 36 39 … … 47 50 self.initial = initial or {} 48 51 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() 49 58 50 59 def __unicode__(self): … … 86 95 return self.prefix and ('%s-%s' % (self.prefix, field_name)) or field_name 87 96 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): 89 98 "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()." 90 99 top_errors = self.non_field_errors() # Errors that should be displayed above all fields. … … 101 110 output.append(error_row % bf_errors) 102 111 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}) 104 117 if top_errors: 105 118 output.insert(0, error_row % top_errors) … … 116 129 def as_table(self): 117 130 "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) 119 132 120 133 def as_ul(self): 121 134 "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) 123 136 124 137 def as_p(self): 125 138 "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) 127 140 128 141 def non_field_errors(self): django/branches/newforms-admin/django/newforms/models.py
r4388 r4441 16 16 if self.errors: 17 17 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) 22 19 23 20 def save_instance(form, instance, commit=True): … … 34 31 raise ValueError("The %s could not be changed because the data didn't validate." % opts.object_name) 35 32 clean_data = form.clean_data 36 for f in opts.fields + opts.many_to_many:33 for f in opts.fields: 37 34 if isinstance(f, models.AutoField): 38 35 continue … … 40 37 if commit: 41 38 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. 42 45 return instance 43 46 … … 65 68 field_list.append((f.name, formfield)) 66 69 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}) 68 71 69 72 def form_for_instance(instance, form=BaseForm, formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): … … 88 91 fields = SortedDictFromList(field_list) 89 92 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)}) 91 94 92 95 def form_for_fields(field_list): 93 96 "Returns a Form class for the given list of Django database field instances." 94 97 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 137 137 class Select(Widget): 138 138 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) 142 144 143 145 def render(self, name, value, attrs=None, choices=()): … … 257 259 def render(self, name, value, attrs=None, choices=()): 258 260 if value is None: value = [] 261 has_id = attrs and attrs.has_key('id') 259 262 final_attrs = self.build_attrs(attrs, name=name) 260 263 output = [u'<ul>'] 261 264 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) 264 271 option_value = smart_unicode(option_value) 265 272 rendered_cb = cb.render(name, option_value) django/branches/newforms-admin/docs/newforms.txt
r4410 r4441 740 740 rendering this ``Field``. See _`Widgets` below for more information. 741 741 742 ``help_text`` 743 ~~~~~~~~~~~~~ 744 745 The ``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 749 Here's a full example ``Form`` that implements ``help_text`` for two of its 750 fields. 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 742 774 Dynamic initial values 743 775 ---------------------- django/branches/newforms-admin/tests/modeltests/model_forms/models.py
r4305 r4441 225 225 </select></li> 226 226 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 231 1 232 >>> new_art = Article.objects.get(id=1) 233 >>> new_art.categories.all() 234 [<Category: Entertainment>, <Category: It's a test>] 235 236 Now, 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 241 1 242 >>> new_art = Article.objects.get(id=1) 243 >>> new_art.categories.all() 244 [] 245 246 Create 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 252 2 253 >>> new_art = Article.objects.get(id=2) 254 >>> new_art.categories.all() 255 [<Category: Entertainment>, <Category: It's a test>] 256 257 Create 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 263 3 264 >>> new_art = Article.objects.get(id=3) 265 >>> new_art.categories.all() 266 [] 267 227 268 Here, we define a custom Form. Because it happens to have the same fields as 228 269 the Category model, we can use save_instance() to apply its changes to an django/branches/newforms-admin/tests/regressiontests/forms/tests.py
r4413 r4441 336 336 >>> w.render('email', 'ŠĐĆŽćžšđ', choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]) 337 337 u'<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 339 If choices is passed to the constructor and is a generator, it can be iterated 340 over 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> 338 358 339 359 # NullBooleanSelect Widget #################################################### … … 2128 2148 <li><label><input checked="checked" type="checkbox" name="composers" value="J" /> John Lennon</label></li> 2129 2149 <li><label><input checked="checked" type="checkbox" name="composers" value="P" /> Paul McCartney</label></li> 2150 </ul> 2151 2152 Regarding auto_id, CheckboxSelectMultiple is a special case. Each checkbox 2153 gets a distinct ID, formed by appending an underscore plus the checkbox's 2154 zero-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> 2130 2160 </ul> 2131 2161 … … 2248 2278 {'username': u'adrian', 'password1': u'foo', 'password2': u'foo'} 2249 2279 2280 # Dynamic construction ######################################################## 2281 2250 2282 It's possible to construct a Form dynamically by adding to the self.fields 2251 2283 dictionary in __init__(). Don't forget to call Form.__init__() within the … … 2262 2294 <tr><th>Last name:</th><td><input type="text" name="last_name" /></td></tr> 2263 2295 <tr><th>Birthday:</th><td><input type="text" name="birthday" /></td></tr> 2296 2297 Instances of a dynamic Form do not persist fields from one Form instance to 2298 the 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> 2264 2336 2265 2337 HiddenInput widgets are displayed differently in the as_table(), as_ul() … … 2539 2611 <li>Password: <input type="password" name="password" /></li> 2540 2612 2613 # Help text ################################################################### 2614 2615 You can specify descriptive text for a field by using the 'help_text' argument 2616 to 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 2631 The 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 2637 help_text is not displayed for hidden fields. It can be used for documentation 2638 purposes, 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 2541 2648 # Forms with prefixes ######################################################### 2542 2649
