From 6245816dd90b8b0b9b8739f1fc292547a1a55639 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Thu, 15 Feb 2007 04:03:47 +0000 Subject: [PATCH] Fixed #3456 -- Made it easier/more intuitive to render newforms help_text in templates git-svn-id: http://code.djangoproject.com/svn/django/trunk@4521 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/forms.py | 1 + tests/regressiontests/forms/tests.py | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 0ce24d3004..c5e4513f9c 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -218,6 +218,7 @@ class BoundField(StrAndUnicode): self.label = pretty_name(name) else: self.label = self.field.label + self.help_text = field.help_text def __unicode__(self): "Renders this field as an HTML widget." diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 08e53f617d..41890e86ef 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -2936,7 +2936,7 @@ VALID: {'username': u'adrian', 'password1': u'secret', 'password2': u'secret'} # Some ideas for using templates with forms ################################### >>> class UserRegistration(Form): -... username = CharField(max_length=10) +... username = CharField(max_length=10, help_text="Good luck picking a username that doesn't already exist.") ... password1 = CharField(widget=PasswordInput) ... password2 = CharField(widget=PasswordInput) ... def clean(self): @@ -3013,6 +3013,22 @@ field an "id" attribute. +User form.[field].help_text to output a field's help text. If the given field +does not have help text, nothing will be output. +>>> t = Template('''
+...

{{ form.username.label_tag }}: {{ form.username }}
{{ form.username.help_text }}

+...

{{ form.password1.label_tag }}: {{ form.password1 }}

+...

{{ form.password2.label_tag }}: {{ form.password2 }}

+... +...
''') +>>> print t.render(Context({'form': UserRegistration(auto_id=False)})) +
+

Username:
Good luck picking a username that doesn't already exist.

+

Password1:

+

Password2:

+ +
+ The label_tag() method takes an optional attrs argument: a dictionary of HTML attributes to add to the