From cf75fcc8321b822cb4758d167f1fade56a60ad4f Mon Sep 17 00:00:00 2001
From: Adrian Holovaty
Date: Sun, 28 Jan 2007 22:10:04 +0000
Subject: [PATCH] Fixed #3255 -- Added help_text argument to newforms Field
class.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4440 bcc190cf-cafb-0310-a4f2-bffc1f526a37
---
django/newforms/fields.py | 62 ++++++++++++++--------------
django/newforms/forms.py | 14 ++++---
docs/newforms.txt | 32 ++++++++++++++
tests/regressiontests/forms/tests.py | 35 ++++++++++++++++
4 files changed, 107 insertions(+), 36 deletions(-)
diff --git a/django/newforms/fields.py b/django/newforms/fields.py
index bcd30dbc92..e9e1fb7746 100644
--- a/django/newforms/fields.py
+++ b/django/newforms/fields.py
@@ -35,7 +35,7 @@ class Field(object):
# Tracks each time a Field instance is created. Used to retain order.
creation_counter = 0
- def __init__(self, required=True, widget=None, label=None, initial=None):
+ def __init__(self, required=True, widget=None, label=None, initial=None, help_text=None):
# required -- Boolean that specifies whether the field is required.
# True by default.
# widget -- A Widget class, or instance of a Widget class, that should be
@@ -47,9 +47,11 @@ class Field(object):
# field name, if the Field is part of a Form.
# initial -- A value to use in this Field's initial display. This value is
# *not* used as a fallback if data isn't given.
+ # help_text -- An optional string to use as "help text" for this Field.
if label is not None:
label = smart_unicode(label)
self.required, self.label, self.initial = required, label, initial
+ self.help_text = help_text
widget = widget or self.widget
if isinstance(widget, type):
widget = widget()
@@ -85,9 +87,9 @@ class Field(object):
return {}
class CharField(Field):
- def __init__(self, max_length=None, min_length=None, required=True, widget=None, label=None, initial=None):
+ def __init__(self, max_length=None, min_length=None, *args, **kwargs):
self.max_length, self.min_length = max_length, min_length
- super(CharField, self).__init__(required, widget, label, initial)
+ super(CharField, self).__init__(*args, **kwargs)
def clean(self, value):
"Validates max_length and min_length. Returns a Unicode object."
@@ -106,9 +108,9 @@ class CharField(Field):
return {'maxlength': str(self.max_length)}
class IntegerField(Field):
- def __init__(self, max_value=None, min_value=None, required=True, widget=None, label=None, initial=None):
+ def __init__(self, max_value=None, min_value=None, *args, **kwargs):
self.max_value, self.min_value = max_value, min_value
- super(IntegerField, self).__init__(required, widget, label, initial)
+ super(IntegerField, self).__init__(*args, **kwargs)
def clean(self, value):
"""
@@ -137,8 +139,8 @@ DEFAULT_DATE_INPUT_FORMATS = (
)
class DateField(Field):
- def __init__(self, input_formats=None, required=True, widget=None, label=None, initial=None):
- super(DateField, self).__init__(required, widget, label, initial)
+ def __init__(self, input_formats=None, *args, **kwargs):
+ super(DateField, self).__init__(*args, **kwargs)
self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS
def clean(self, value):
@@ -166,8 +168,8 @@ DEFAULT_TIME_INPUT_FORMATS = (
)
class TimeField(Field):
- def __init__(self, input_formats=None, required=True, widget=None, label=None, initial=None):
- super(TimeField, self).__init__(required, widget, label, initial)
+ def __init__(self, input_formats=None, *args, **kwargs):
+ super(TimeField, self).__init__(*args, **kwargs)
self.input_formats = input_formats or DEFAULT_TIME_INPUT_FORMATS
def clean(self, value):
@@ -200,8 +202,8 @@ DEFAULT_DATETIME_INPUT_FORMATS = (
)
class DateTimeField(Field):
- def __init__(self, input_formats=None, required=True, widget=None, label=None, initial=None):
- super(DateTimeField, self).__init__(required, widget, label, initial)
+ def __init__(self, input_formats=None, *args, **kwargs):
+ super(DateTimeField, self).__init__(*args, **kwargs)
self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS
def clean(self, value):
@@ -224,14 +226,13 @@ class DateTimeField(Field):
raise ValidationError(gettext(u'Enter a valid date/time.'))
class RegexField(Field):
- def __init__(self, regex, max_length=None, min_length=None, error_message=None,
- required=True, widget=None, label=None, initial=None):
+ def __init__(self, regex, max_length=None, min_length=None, error_message=None, *args, **kwargs):
"""
regex can be either a string or a compiled regular expression object.
error_message is an optional error message to use, if
'Enter a valid value' is too generic for you.
"""
- super(RegexField, self).__init__(required, widget, label, initial)
+ super(RegexField, self).__init__(*args, **kwargs)
if isinstance(regex, basestring):
regex = re.compile(regex)
self.regex = regex
@@ -263,8 +264,9 @@ email_re = re.compile(
r')@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$', re.IGNORECASE) # domain
class EmailField(RegexField):
- def __init__(self, max_length=None, min_length=None, required=True, widget=None, label=None, initial=None):
- RegexField.__init__(self, email_re, max_length, min_length, gettext(u'Enter a valid e-mail address.'), required, widget, label, initial)
+ def __init__(self, max_length=None, min_length=None, *args, **kwargs):
+ RegexField.__init__(self, email_re, max_length, min_length,
+ gettext(u'Enter a valid e-mail address.'), *args, **kwargs)
url_re = re.compile(
r'^https?://' # http:// or https://
@@ -280,9 +282,9 @@ except ImportError:
URL_VALIDATOR_USER_AGENT = 'Django (http://www.djangoproject.com/)'
class URLField(RegexField):
- def __init__(self, max_length=None, min_length=None, required=True, verify_exists=False, widget=None, label=None,
- initial=None, validator_user_agent=URL_VALIDATOR_USER_AGENT):
- super(URLField, self).__init__(url_re, max_length, min_length, gettext(u'Enter a valid URL.'), required, widget, label, initial)
+ def __init__(self, max_length=None, min_length=None, verify_exists=False,
+ validator_user_agent=URL_VALIDATOR_USER_AGENT, *args, **kwargs):
+ super(URLField, self).__init__(url_re, max_length, min_length, gettext(u'Enter a valid URL.'), *args, **kwargs)
self.verify_exists = verify_exists
self.user_agent = validator_user_agent
@@ -328,10 +330,8 @@ class NullBooleanField(BooleanField):
return {True: True, False: False}.get(value, None)
class ChoiceField(Field):
- def __init__(self, choices=(), required=True, widget=Select, label=None, initial=None):
- if isinstance(widget, type):
- widget = widget()
- super(ChoiceField, self).__init__(required, widget, label, initial)
+ def __init__(self, choices=(), required=True, widget=Select, label=None, initial=None, help_text=None):
+ super(ChoiceField, self).__init__(required, widget, label, initial, help_text)
self.choices = choices
def _get_choices(self):
@@ -362,8 +362,8 @@ class ChoiceField(Field):
class MultipleChoiceField(ChoiceField):
hidden_widget = MultipleHiddenInput
- def __init__(self, choices=(), required=True, widget=SelectMultiple, label=None, initial=None):
- super(MultipleChoiceField, self).__init__(choices, required, widget, label, initial)
+ def __init__(self, choices=(), required=True, widget=SelectMultiple, label=None, initial=None, help_text=None):
+ super(MultipleChoiceField, self).__init__(choices, required, widget, label, initial, help_text)
def clean(self, value):
"""
@@ -390,8 +390,8 @@ class ComboField(Field):
"""
A Field whose clean() method calls multiple Field clean() methods.
"""
- def __init__(self, fields=(), required=True, widget=None, label=None, initial=None):
- super(ComboField, self).__init__(required, widget, label, initial)
+ def __init__(self, fields=(), *args, **kwargs):
+ super(ComboField, self).__init__(*args, **kwargs)
# Set 'required' to False on the individual fields, because the
# required validation will be handled by ComboField, not by those
# individual fields.
@@ -425,8 +425,8 @@ class MultiValueField(Field):
You'll probably want to use this with MultiWidget.
"""
- def __init__(self, fields=(), required=True, widget=None, label=None, initial=None):
- super(MultiValueField, self).__init__(required, widget, label, initial)
+ def __init__(self, fields=(), *args, **kwargs):
+ super(MultiValueField, self).__init__(*args, **kwargs)
# Set 'required' to False on the individual fields, because the
# required validation will be handled by MultiValueField, not by those
# individual fields.
@@ -481,9 +481,9 @@ class MultiValueField(Field):
raise NotImplementedError('Subclasses must implement this method.')
class SplitDateTimeField(MultiValueField):
- def __init__(self, required=True, widget=None, label=None, initial=None):
+ def __init__(self, *args, **kwargs):
fields = (DateField(), TimeField())
- super(SplitDateTimeField, self).__init__(fields, required, widget, label, initial)
+ super(SplitDateTimeField, self).__init__(fields, *args, **kwargs)
def compress(self, data_list):
if data_list:
diff --git a/django/newforms/forms.py b/django/newforms/forms.py
index 3536f942e9..7e29941bef 100644
--- a/django/newforms/forms.py
+++ b/django/newforms/forms.py
@@ -94,7 +94,7 @@ class BaseForm(StrAndUnicode):
"""
return self.prefix and ('%s-%s' % (self.prefix, field_name)) or field_name
- def _html_output(self, normal_row, error_row, row_ender, errors_on_separate_row):
+ def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
"Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
output, hidden_fields = [], []
@@ -109,7 +109,11 @@ class BaseForm(StrAndUnicode):
if errors_on_separate_row and bf_errors:
output.append(error_row % bf_errors)
label = bf.label and bf.label_tag(escape(bf.label + ':')) or ''
- output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf)})
+ if field.help_text:
+ help_text = help_text_html % field.help_text
+ else:
+ help_text = u''
+ output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text})
if top_errors:
output.insert(0, error_row % top_errors)
if hidden_fields: # Insert any hidden fields in the last row.
@@ -124,15 +128,15 @@ class BaseForm(StrAndUnicode):
def as_table(self):
"Returns this form rendered as HTML
s -- excluding the
."
- return self._html_output(u'
%(label)s
%(errors)s%(field)s
', u'
%s
', '', False)
+ return self._html_output(u'
%(label)s
%(errors)s%(field)s%(help_text)s
', u'
%s
', '', u' %s', False)
def as_ul(self):
"Returns this form rendered as HTML
s -- excluding the
."
- return self._html_output(u'
%(errors)s%(label)s %(field)s
', u'
%s
', '', False)
+ return self._html_output(u'
%(errors)s%(label)s %(field)s%(help_text)s
', u'
%s
', '', u' %s', False)
def as_p(self):
"Returns this form rendered as HTML
s."
- return self._html_output(u'
%(label)s %(field)s
', u'
%s
', '
', True)
+ return self._html_output(u'
%(label)s %(field)s%(help_text)s
', u'
%s
', '', u' %s', True)
def non_field_errors(self):
"""
diff --git a/docs/newforms.txt b/docs/newforms.txt
index aeed33752a..063f686ed5 100644
--- a/docs/newforms.txt
+++ b/docs/newforms.txt
@@ -739,6 +739,38 @@ validation if a particular field's value is not given. ``initial`` values are
The ``widget`` argument lets you specify a ``Widget`` class to use when
rendering this ``Field``. See _`Widgets` below for more information.
+``help_text``
+~~~~~~~~~~~~~
+
+The ``help_text`` argument lets you specify descriptive text for this
+``Field``. If you provide ``help_text``, it will be displayed next to the
+``Field`` when the ``Field`` is rendered in a ``Form``.
+
+Here's a full example ``Form`` that implements ``help_text`` for two of its
+fields. We've specified ``auto_id=False`` to simplify the output::
+
+ >>> class HelpTextContactForm(forms.Form):
+ ... subject = forms.CharField(max_length=100, help_text='100 characters max.')
+ ... message = forms.CharField()
+ ... sender = forms.EmailField(help_text='A valid e-mail address, please.')
+ ... cc_myself = forms.BooleanField()
+ >>> f = HelpTextContactForm(auto_id=False)
+ >>> print f.as_table()
+
Subject:
100 characters max.
+
Message:
+
Sender:
A valid e-mail address, please.
+
Cc myself:
+ >>> print f.as_ul()
+
Subject: 100 characters max.
+
Message:
+
Sender: A valid e-mail address, please.
+
Cc myself:
+ >>> print f.as_p()
+
Subject: 100 characters max.
+
Message:
+
Sender: A valid e-mail address, please.
+
Cc myself:
+
Dynamic initial values
----------------------
diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py
index 5c9c1ed813..20a1937f56 100644
--- a/tests/regressiontests/forms/tests.py
+++ b/tests/regressiontests/forms/tests.py
@@ -2610,6 +2610,41 @@ then the latter will get precedence.
Username:
Password:
+# Help text ###################################################################
+
+You can specify descriptive text for a field by using the 'help_text' argument
+to a Field class. This help text is displayed when a Form is rendered.
+>>> class UserRegistration(Form):
+... username = CharField(max_length=10, help_text='e.g., user@example.com')
+... password = CharField(widget=PasswordInput, help_text='Choose wisely.')
+>>> p = UserRegistration(auto_id=False)
+>>> print p.as_ul()
+
Username: e.g., user@example.com
+
Password: Choose wisely.
+>>> print p.as_p()
+
Username: e.g., user@example.com
+
Password: Choose wisely.
+>>> print p.as_table()
+
Username:
e.g., user@example.com
+
Password:
Choose wisely.
+
+The help text is displayed whether or not data is provided for the form.
+>>> p = UserRegistration({'username': u'foo'}, auto_id=False)
+>>> print p.as_ul()
+
Username: e.g., user@example.com
+
This field is required.
Password: Choose wisely.
+
+help_text is not displayed for hidden fields. It can be used for documentation
+purposes, though.
+>>> class UserRegistration(Form):
+... username = CharField(max_length=10, help_text='e.g., user@example.com')
+... password = CharField(widget=PasswordInput)
+... next = CharField(widget=HiddenInput, initial='/', help_text='Redirect destination')
+>>> p = UserRegistration(auto_id=False)
+>>> print p.as_ul()
+
Username: e.g., user@example.com
+
Password:
+
# Forms with prefixes #########################################################
Sometimes it's necessary to have multiple forms display on the same HTML page,