Changeset 4188
- Timestamp:
- 12/08/06 14:06:12 (2 years ago)
- Files:
-
- django/trunk/django/newforms/fields.py (modified) (11 diffs)
- django/trunk/django/newforms/forms.py (modified) (3 diffs)
- django/trunk/tests/regressiontests/forms/tests.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/newforms/fields.py
r4187 r4188 33 33 creation_counter = 0 34 34 35 def __init__(self, required=True, widget=None ):36 self.required = required35 def __init__(self, required=True, widget=None, label=None): 36 self.required, self.label = required, label 37 37 widget = widget or self.widget 38 38 if isinstance(widget, type): … … 70 70 71 71 class CharField(Field): 72 def __init__(self, max_length=None, min_length=None, required=True, widget=None ):72 def __init__(self, max_length=None, min_length=None, required=True, widget=None, label=None): 73 73 self.max_length, self.min_length = max_length, min_length 74 Field.__init__(self, required, widget )74 Field.__init__(self, required, widget, label) 75 75 76 76 def clean(self, value): … … 112 112 113 113 class DateField(Field): 114 def __init__(self, input_formats=None, required=True, widget=None ):115 Field.__init__(self, required, widget )114 def __init__(self, input_formats=None, required=True, widget=None, label=None): 115 Field.__init__(self, required, widget, label) 116 116 self.input_formats = input_formats or DEFAULT_DATE_INPUT_FORMATS 117 117 … … 148 148 149 149 class DateTimeField(Field): 150 def __init__(self, input_formats=None, required=True, widget=None ):151 Field.__init__(self, required, widget )150 def __init__(self, input_formats=None, required=True, widget=None, label=None): 151 Field.__init__(self, required, widget, label) 152 152 self.input_formats = input_formats or DEFAULT_DATETIME_INPUT_FORMATS 153 153 … … 172 172 173 173 class RegexField(Field): 174 def __init__(self, regex, error_message=None, required=True, widget=None ):174 def __init__(self, regex, error_message=None, required=True, widget=None, label=None): 175 175 """ 176 176 regex can be either a string or a compiled regular expression object. … … 178 178 'Enter a valid value' is too generic for you. 179 179 """ 180 Field.__init__(self, required, widget )180 Field.__init__(self, required, widget, label) 181 181 if isinstance(regex, basestring): 182 182 regex = re.compile(regex) … … 204 204 205 205 class EmailField(RegexField): 206 def __init__(self, required=True, widget=None ):207 RegexField.__init__(self, email_re, gettext(u'Enter a valid e-mail address.'), required, widget )206 def __init__(self, required=True, widget=None, label=None): 207 RegexField.__init__(self, email_re, gettext(u'Enter a valid e-mail address.'), required, widget, label) 208 208 209 209 url_re = re.compile( … … 221 221 222 222 class URLField(RegexField): 223 def __init__(self, required=True, verify_exists=False, widget=None, 223 def __init__(self, required=True, verify_exists=False, widget=None, label=None, 224 224 validator_user_agent=URL_VALIDATOR_USER_AGENT): 225 RegexField.__init__(self, url_re, gettext(u'Enter a valid URL.'), required, widget )225 RegexField.__init__(self, url_re, gettext(u'Enter a valid URL.'), required, widget, label) 226 226 self.verify_exists = verify_exists 227 227 self.user_agent = validator_user_agent … … 257 257 258 258 class ChoiceField(Field): 259 def __init__(self, choices=(), required=True, widget=Select ):259 def __init__(self, choices=(), required=True, widget=Select, label=None): 260 260 if isinstance(widget, type): 261 261 widget = widget(choices=choices) 262 Field.__init__(self, required, widget )262 Field.__init__(self, required, widget, label) 263 263 self.choices = choices 264 264 … … 278 278 279 279 class MultipleChoiceField(ChoiceField): 280 def __init__(self, choices=(), required=True, widget=SelectMultiple ):281 ChoiceField.__init__(self, choices, required, widget )280 def __init__(self, choices=(), required=True, widget=SelectMultiple, label=None): 281 ChoiceField.__init__(self, choices, required, widget, label) 282 282 283 283 def clean(self, value): … … 303 303 304 304 class ComboField(Field): 305 def __init__(self, fields=(), required=True, widget=None ):306 Field.__init__(self, required, widget )305 def __init__(self, fields=(), required=True, widget=None, label=None): 306 Field.__init__(self, required, widget, label) 307 307 # Set 'required' to False on the individual fields, because the 308 308 # required validation will be handled by ComboField, not by those django/trunk/django/newforms/forms.py
r4182 r4188 87 87 if errors_on_separate_row and bf_errors: 88 88 output.append(error_row % bf_errors) 89 output.append(normal_row % {'errors': bf_errors, 'label': bf.label_tag(escape(bf. verbose_name+':')), 'field': bf})89 output.append(normal_row % {'errors': bf_errors, 'label': bf.label_tag(escape(bf.label+':')), 'field': bf}) 90 90 if top_errors: 91 91 output.insert(0, error_row % top_errors) … … 165 165 self.field = field 166 166 self.name = name 167 self.label = self.field.label or pretty_name(name) 167 168 168 169 def __unicode__(self): … … 214 215 data = property(_data) 215 216 216 def _verbose_name(self):217 return pretty_name(self.name)218 verbose_name = property(_verbose_name)219 220 217 def label_tag(self, contents=None): 221 218 """ 222 219 Wraps the given contents in a <label>, if the field has an ID attribute. 223 220 Does not HTML-escape the contents. If contents aren't given, uses the 224 field's HTML-escaped verbose_name.225 """ 226 contents = contents or escape(self. verbose_name)221 field's HTML-escaped label. 222 """ 223 contents = contents or escape(self.label) 227 224 widget = self.field.widget 228 225 id_ = widget.attrs.get('id') or self.auto_id django/trunk/tests/regressiontests/forms/tests.py
r4187 r4188 637 637 Widget that it'll use if you don't specify this. In most cases, 638 638 the default widget is TextInput. 639 label -- A verbose name for this field, for use in displaying this field in 640 a form. By default, Django will use a "pretty" version of the form 641 field name, if the Field is part of a Form. 639 642 640 643 Other than that, the Field subclasses have class-specific options for … … 1336 1339 <input type="text" name="birthday" value="1940-10-9" /> 1337 1340 >>> for boundfield in p: 1338 ... print boundfield. verbose_name, boundfield.data1341 ... print boundfield.label, boundfield.data 1339 1342 First name John 1340 1343 Last name Lennon … … 1909 1912 <li>Password: <input type="password" name="password" maxlength="10" /></li> 1910 1913 1914 You can specify the label for a field by using the 'label' argument to a Field 1915 class. If you don't specify 'label', Django will use the field name with 1916 underscores converted to spaces, and the initial letter capitalized. 1917 >>> class UserRegistration(Form): 1918 ... username = CharField(max_length=10, label='Your username') 1919 ... password1 = CharField(widget=PasswordInput) 1920 ... password2 = CharField(widget=PasswordInput, label='Password (again)') 1921 >>> p = UserRegistration() 1922 >>> print p.as_ul() 1923 <li>Your username: <input type="text" name="username" maxlength="10" /></li> 1924 <li>Password1: <input type="password" name="password1" /></li> 1925 <li>Password (again): <input type="password" name="password2" /></li> 1926 1911 1927 # Basic form processing in a view ############################################# 1912 1928 … … 1995 2011 </form> 1996 2012 1997 Use form.[field].verbose_name to output a field's "verbose name" -- its field 1998 name with underscores converted to spaces, and the initial letter capitalized. 2013 Use form.[field].label to output a field's label. You can specify the label for 2014 a field by using the 'label' argument to a Field class. If you don't specify 2015 'label', Django will use the field name with underscores converted to spaces, 2016 and the initial letter capitalized. 1999 2017 >>> t = Template('''<form action=""> 2000 ... <p><label>{{ form.username. verbose_name}}: {{ form.username }}</label></p>2001 ... <p><label>{{ form.password1. verbose_name}}: {{ form.password1 }}</label></p>2002 ... <p><label>{{ form.password2. verbose_name}}: {{ form.password2 }}</label></p>2018 ... <p><label>{{ form.username.label }}: {{ form.username }}</label></p> 2019 ... <p><label>{{ form.password1.label }}: {{ form.password1 }}</label></p> 2020 ... <p><label>{{ form.password2.label }}: {{ form.password2 }}</label></p> 2003 2021 ... <input type="submit" /> 2004 2022 ... </form>''') … … 2011 2029 </form> 2012 2030 2013 User form.[field].label_tag to output a field's verbose_name with a <label>2014 tagwrapped around it, but *only* if the given field has an "id" attribute.2031 User form.[field].label_tag to output a field's label with a <label> tag 2032 wrapped around it, but *only* if the given field has an "id" attribute. 2015 2033 Recall from above that passing the "auto_id" argument to a Form gives each 2016 2034 field an "id" attribute.
