Ticket #3064: newforms_label.patch

File newforms_label.patch, 4.2 KB (added by Chris Beaven, 18 years ago)
  • django/newforms/forms.py

     
    33"""
    44
    55from django.utils.datastructures import SortedDict
     6from django.utils.html import escape
    67from fields import Field
    78from widgets import TextInput, Textarea
    89from util import ErrorDict, ErrorList, ValidationError
     
    190191        "Returns a string of HTML for representing this as a <textarea>."
    191192        return self.as_widget(Textarea(), attrs)
    192193
     194    def _verbose_name(self):
     195        return pretty_name(self._name)
     196    verbose_name = property(_verbose_name)
     197
    193198    def _label(self):
    194         return pretty_name(self._name)
     199        """
     200        Returns the verbose field name (HTML escaped) and wraps with an HTML
     201        label tag if either the field has an auto_id or the default widget has
     202        an id attribute.
     203        """
     204        label = escape(self.verbose_name)
     205        id = self._field.widget.attrs.get('id') or self.auto_id
     206        if id:
     207            label = '<label for="%s">%s</label>' % (escape(id), label)
     208        return label
    195209    label = property(_label)
    196210
    197211    def _auto_id(self):
  • tests/regressiontests/forms/tests.py

     
    12521252"auto_id" tells the Form to add an "id" attribute to each form element.
    12531253If it's a string that contains '%s', Django will use that as a format string
    12541254into which the field's name will be inserted.
     1255Form elements with ids also get html label tags.
    12551256>>> p = Person(auto_id='id_%s')
    12561257>>> print p.as_ul()
    1257 <li>First name: <input type="text" name="first_name" id="id_first_name" /></li>
    1258 <li>Last name: <input type="text" name="last_name" id="id_last_name" /></li>
    1259 <li>Birthday: <input type="text" name="birthday" id="id_birthday" /></li>
     1258<li><label for="id_first_name">First name</label>: <input type="text" name="first_name" id="id_first_name" /></li>
     1259<li><label for="id_last_name">Last name</label>: <input type="text" name="last_name" id="id_last_name" /></li>
     1260<li><label for="id_birthday">Birthday</label>: <input type="text" name="birthday" id="id_birthday" /></li>
    12601261
    12611262If auto_id is any True value whose str() does not contain '%s', the "id"
    12621263attribute will be the name of the field.
    12631264>>> p = Person(auto_id=True)
    12641265>>> print p.as_ul()
    1265 <li>First name: <input type="text" name="first_name" id="first_name" /></li>
    1266 <li>Last name: <input type="text" name="last_name" id="last_name" /></li>
    1267 <li>Birthday: <input type="text" name="birthday" id="birthday" /></li>
     1266<li><label for="first_name">First name</label>: <input type="text" name="first_name" id="first_name" /></li>
     1267<li><label for="last_name">Last name</label>: <input type="text" name="last_name" id="last_name" /></li>
     1268<li><label for="birthday">Birthday</label>: <input type="text" name="birthday" id="birthday" /></li>
    12681269
    12691270If auto_id is any False value, an "id" attribute won't be output unless it
    12701271was manually entered.
     
    12821283...     birthday = DateField()
    12831284>>> p = PersonNew(auto_id=False)
    12841285>>> print p.as_ul()
    1285 <li>First name: <input type="text" id="first_name_id" name="first_name" /></li>
     1286<li><label for="first_name_id">First name</label>: <input type="text" id="first_name_id" name="first_name" /></li>
    12861287<li>Last name: <input type="text" name="last_name" /></li>
    12871288<li>Birthday: <input type="text" name="birthday" /></li>
    12881289
     
    12901291attribute in the Form gets precedence.
    12911292>>> p = PersonNew(auto_id=True)
    12921293>>> print p.as_ul()
    1293 <li>First name: <input type="text" id="first_name_id" name="first_name" /></li>
    1294 <li>Last name: <input type="text" name="last_name" id="last_name" /></li>
    1295 <li>Birthday: <input type="text" name="birthday" id="birthday" /></li>
     1294<li><label for="first_name_id">First name</label>: <input type="text" id="first_name_id" name="first_name" /></li>
     1295<li><label for="last_name">Last name</label>: <input type="text" name="last_name" id="last_name" /></li>
     1296<li><label for="birthday">Birthday</label>: <input type="text" name="birthday" id="birthday" /></li>
    12961297
    12971298>>> class SignupForm(Form):
    12981299...     email = EmailField()
Back to Top