diff options
| author | Adrian Holovaty <adrian@holovaty.com> | 2007-01-20 20:31:17 +0000 |
|---|---|---|
| committer | Adrian Holovaty <adrian@holovaty.com> | 2007-01-20 20:31:17 +0000 |
| commit | a154d94e456af97a05a6d630bd8ec7193d67684b (patch) | |
| tree | 307adac444a53a65de4b73eff55a3099ea25b3fb | |
| parent | 8f8b77bb5526bc54f5b66f6702848fc940ab8e18 (diff) | |
newforms: Added attrs optional argument to BoundForm.label_tag()
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4369 bcc190cf-cafb-0310-a4f2-bffc1f526a37
| -rw-r--r-- | django/newforms/forms.py | 9 | ||||
| -rw-r--r-- | tests/regressiontests/forms/tests.py | 9 |
2 files changed, 15 insertions, 3 deletions
diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 0b4687bcf3..5964c9cc5e 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -5,7 +5,7 @@ Form classes from django.utils.datastructures import SortedDict, MultiValueDict from django.utils.html import escape from fields import Field -from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput +from widgets import flatatt, TextInput, Textarea, HiddenInput, MultipleHiddenInput from util import StrAndUnicode, ErrorDict, ErrorList, ValidationError __all__ = ('BaseForm', 'Form') @@ -247,17 +247,20 @@ class BoundField(StrAndUnicode): return self.field.widget.value_from_datadict(self.form.data, self.html_name) data = property(_data) - def label_tag(self, contents=None): + def label_tag(self, contents=None, attrs=None): """ Wraps the given contents in a <label>, if the field has an ID attribute. Does not HTML-escape the contents. If contents aren't given, uses the field's HTML-escaped label. + + If attrs are given, they're used as HTML attributes on the <label> tag. """ contents = contents or escape(self.label) widget = self.field.widget id_ = widget.attrs.get('id') or self.auto_id if id_: - contents = '<label for="%s">%s</label>' % (widget.id_for_label(id_), contents) + attrs = attrs and flatatt(attrs) or '' + contents = '<label for="%s"%s>%s</label>' % (widget.id_for_label(id_), attrs, contents) return contents def _is_hidden(self): diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 2ae10dc0d6..336edf6cb7 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -2593,6 +2593,15 @@ field an "id" attribute. <input type="submit" /> </form> +The label_tag() method takes an optional attrs argument: a dictionary of HTML +attributes to add to the <label> tag. +>>> f = UserRegistration(auto_id='id_%s') +>>> for bf in f: +... print bf.label_tag(attrs={'class': 'pretty'}) +<label for="id_username" class="pretty">Username</label> +<label for="id_password1" class="pretty">Password1</label> +<label for="id_password2" class="pretty">Password2</label> + To display the errors that aren't associated with a particular field -- e.g., the errors caused by Form.clean() -- use {{ form.non_field_errors }} in the template. If used on its own, it is displayed as a <ul> (or an empty string, if |
