Django

Code

Changeset 4240

Show
Ignore:
Timestamp:
12/26/06 17:33:20 (2 years ago)
Author:
adrian
Message:

newforms: A label can now be the empty string, in which case a label won't be displayed

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/newforms/forms.py

    r4239 r4240  
    100100                if errors_on_separate_row and bf_errors: 
    101101                    output.append(error_row % bf_errors) 
    102                 output.append(normal_row % {'errors': bf_errors, 'label': bf.label_tag(escape(bf.label+':')), 'field': bf}) 
     102                label = bf.label and bf.label_tag(escape(bf.label + ':')) or '' 
     103                output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': bf}) 
    103104        if top_errors: 
    104105            output.insert(0, error_row % top_errors) 
     
    188189        self.name = name 
    189190        self.html_name = form.add_prefix(name) 
    190         self.label = self.field.label or pretty_name(name) 
     191        if self.field.label is None: 
     192            self.label = pretty_name(name) 
     193        else: 
     194            self.label = self.field.label 
    191195 
    192196    def __unicode__(self): 
  • django/trunk/tests/regressiontests/forms/tests.py

    r4239 r4240  
    20812081u'<li>\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111: <input type="text" name="username" maxlength="10" /></li>\n<li>\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111: <input type="password" name="password" /></li>' 
    20822082 
     2083If a label is set to the empty string for a field, that field won't get a label. 
     2084>>> class UserRegistration(Form): 
     2085...    username = CharField(max_length=10, label='') 
     2086...    password = CharField(widget=PasswordInput) 
     2087>>> p = UserRegistration(auto_id=False) 
     2088>>> print p.as_ul() 
     2089<li> <input type="text" name="username" maxlength="10" /></li> 
     2090<li>Password: <input type="password" name="password" /></li> 
     2091>>> p = UserRegistration(auto_id='id_%s') 
     2092>>> print p.as_ul() 
     2093<li> <input id="id_username" type="text" name="username" maxlength="10" /></li> 
     2094<li><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li> 
     2095 
     2096If label is None, Django will auto-create the label from the field name. This 
     2097is default behavior. 
     2098>>> class UserRegistration(Form): 
     2099...    username = CharField(max_length=10, label=None) 
     2100...    password = CharField(widget=PasswordInput) 
     2101>>> p = UserRegistration(auto_id=False) 
     2102>>> print p.as_ul() 
     2103<li>Username: <input type="text" name="username" maxlength="10" /></li> 
     2104<li>Password: <input type="password" name="password" /></li> 
     2105>>> p = UserRegistration(auto_id='id_%s') 
     2106>>> print p.as_ul() 
     2107<li><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></li> 
     2108<li><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li> 
     2109 
    20832110# Forms with prefixes ######################################################### 
    20842111