Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit a154d94e456af97a05a6d630bd8ec7193d67684b 1 parent 8f8b77b
Adrian Holovaty authored
9  django/newforms/forms.py
@@ -5,7 +5,7 @@
5 5
 from django.utils.datastructures import SortedDict, MultiValueDict
6 6
 from django.utils.html import escape
7 7
 from fields import Field
8  
-from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput
  8
+from widgets import flatatt, TextInput, Textarea, HiddenInput, MultipleHiddenInput
9 9
 from util import StrAndUnicode, ErrorDict, ErrorList, ValidationError
10 10
 
11 11
 __all__ = ('BaseForm', 'Form')
@@ -247,17 +247,20 @@ def _data(self):
247 247
         return self.field.widget.value_from_datadict(self.form.data, self.html_name)
248 248
     data = property(_data)
249 249
 
250  
-    def label_tag(self, contents=None):
  250
+    def label_tag(self, contents=None, attrs=None):
251 251
         """
252 252
         Wraps the given contents in a <label>, if the field has an ID attribute.
253 253
         Does not HTML-escape the contents. If contents aren't given, uses the
254 254
         field's HTML-escaped label.
  255
+
  256
+        If attrs are given, they're used as HTML attributes on the <label> tag.
255 257
         """
256 258
         contents = contents or escape(self.label)
257 259
         widget = self.field.widget
258 260
         id_ = widget.attrs.get('id') or self.auto_id
259 261
         if id_:
260  
-            contents = '<label for="%s">%s</label>' % (widget.id_for_label(id_), contents)
  262
+            attrs = attrs and flatatt(attrs) or ''
  263
+            contents = '<label for="%s"%s>%s</label>' % (widget.id_for_label(id_), attrs, contents)
261 264
         return contents
262 265
 
263 266
     def _is_hidden(self):
9  tests/regressiontests/forms/tests.py
@@ -2593,6 +2593,15 @@
2593 2593
 <input type="submit" />
2594 2594
 </form>
2595 2595
 
  2596
+The label_tag() method takes an optional attrs argument: a dictionary of HTML
  2597
+attributes to add to the <label> tag.
  2598
+>>> f = UserRegistration(auto_id='id_%s')
  2599
+>>> for bf in f:
  2600
+...     print bf.label_tag(attrs={'class': 'pretty'})
  2601
+<label for="id_username" class="pretty">Username</label>
  2602
+<label for="id_password1" class="pretty">Password1</label>
  2603
+<label for="id_password2" class="pretty">Password2</label>
  2604
+
2596 2605
 To display the errors that aren't associated with a particular field -- e.g.,
2597 2606
 the errors caused by Form.clean() -- use {{ form.non_field_errors }} in the
2598 2607
 template. If used on its own, it is displayed as a <ul> (or an empty string, if

0 notes on commit a154d94

Please sign in to comment.
Something went wrong with that request. Please try again.