diff --git a/django/forms/fields.py b/django/forms/fields.py
index c547d14..180b524 100644
a
|
b
|
from django.core import validators
|
19 | 19 | from django.core.exceptions import ValidationError |
20 | 20 | from django.forms.util import ErrorList, from_current_timezone, to_current_timezone |
21 | 21 | from django.forms.widgets import ( |
22 | | TextInput, PasswordInput, EmailInput, URLInput, HiddenInput, |
| 22 | TextInput, NumberInput, EmailInput, URLInput, HiddenInput, |
23 | 23 | MultipleHiddenInput, ClearableFileInput, CheckboxInput, Select, |
24 | 24 | NullBooleanSelect, SelectMultiple, DateInput, DateTimeInput, TimeInput, |
25 | 25 | SplitDateTimeWidget, SplitHiddenDateTimeWidget, FILE_INPUT_CONTRADICTION |
… |
… |
class IntegerField(Field):
|
234 | 234 | |
235 | 235 | def __init__(self, max_value=None, min_value=None, *args, **kwargs): |
236 | 236 | self.max_value, self.min_value = max_value, min_value |
| 237 | kwargs.setdefault('widget', NumberInput if not kwargs.get('localize') else self.widget) |
237 | 238 | super(IntegerField, self).__init__(*args, **kwargs) |
238 | 239 | |
239 | 240 | if max_value is not None: |
… |
… |
class IntegerField(Field):
|
257 | 258 | raise ValidationError(self.error_messages['invalid']) |
258 | 259 | return value |
259 | 260 | |
| 261 | def widget_attrs(self, widget): |
| 262 | attrs = super(IntegerField, self).widget_attrs(widget) |
| 263 | if isinstance(widget, NumberInput): |
| 264 | if self.min_value is not None: |
| 265 | attrs['min'] = self.min_value |
| 266 | if self.max_value is not None: |
| 267 | attrs['max'] = self.max_value |
| 268 | return attrs |
| 269 | |
| 270 | |
260 | 271 | class FloatField(IntegerField): |
261 | 272 | default_error_messages = { |
262 | 273 | 'invalid': _('Enter a number.'), |
… |
… |
class FloatField(IntegerField):
|
278 | 289 | raise ValidationError(self.error_messages['invalid']) |
279 | 290 | return value |
280 | 291 | |
281 | | class DecimalField(Field): |
| 292 | def widget_attrs(self, widget): |
| 293 | attrs = super(FloatField, self).widget_attrs(widget) |
| 294 | if isinstance(widget, NumberInput): |
| 295 | attrs.setdefault('step', 'any') |
| 296 | return attrs |
| 297 | |
| 298 | |
| 299 | class DecimalField(IntegerField): |
282 | 300 | default_error_messages = { |
283 | 301 | 'invalid': _('Enter a number.'), |
284 | | 'max_value': _('Ensure this value is less than or equal to %(limit_value)s.'), |
285 | | 'min_value': _('Ensure this value is greater than or equal to %(limit_value)s.'), |
286 | 302 | 'max_digits': _('Ensure that there are no more than %s digits in total.'), |
287 | 303 | 'max_decimal_places': _('Ensure that there are no more than %s decimal places.'), |
288 | 304 | 'max_whole_digits': _('Ensure that there are no more than %s digits before the decimal point.') |
289 | 305 | } |
290 | 306 | |
291 | 307 | def __init__(self, max_value=None, min_value=None, max_digits=None, decimal_places=None, *args, **kwargs): |
292 | | self.max_value, self.min_value = max_value, min_value |
293 | 308 | self.max_digits, self.decimal_places = max_digits, decimal_places |
294 | | Field.__init__(self, *args, **kwargs) |
295 | | |
296 | | if max_value is not None: |
297 | | self.validators.append(validators.MaxValueValidator(max_value)) |
298 | | if min_value is not None: |
299 | | self.validators.append(validators.MinValueValidator(min_value)) |
| 309 | super(DecimalField, self).__init__(max_value, min_value, *args, **kwargs) |
300 | 310 | |
301 | 311 | def to_python(self, value): |
302 | 312 | """ |
… |
… |
class DecimalField(Field):
|
345 | 355 | raise ValidationError(self.error_messages['max_whole_digits'] % (self.max_digits - self.decimal_places)) |
346 | 356 | return value |
347 | 357 | |
| 358 | def widget_attrs(self, widget): |
| 359 | attrs = super(DecimalField, self).widget_attrs(widget) |
| 360 | if isinstance(widget, NumberInput): |
| 361 | if self.max_digits is not None: |
| 362 | max_length = self.max_digits + 1 # for the sign |
| 363 | if self.decimal_places is None or self.decimal_places > 0: |
| 364 | max_length += 1 # for the dot |
| 365 | attrs['maxlength'] = max_length |
| 366 | if self.decimal_places: |
| 367 | attrs['step'] = '0.%s1' % ('0' * (self.decimal_places-1)) |
| 368 | return attrs |
| 369 | |
| 370 | |
348 | 371 | class BaseTemporalField(Field): |
349 | 372 | |
350 | 373 | def __init__(self, input_formats=None, *args, **kwargs): |
diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index e906ed5..026e8dc 100644
a
|
b
|
from django.utils import datetime_safe, formats, six
|
23 | 23 | |
24 | 24 | __all__ = ( |
25 | 25 | 'Media', 'MediaDefiningClass', 'Widget', 'TextInput', |
26 | | 'EmailInput', 'URLInput', 'PasswordInput', |
| 26 | 'EmailInput', 'URLInput', 'NumberInput', 'PasswordInput', |
27 | 27 | 'HiddenInput', 'MultipleHiddenInput', 'ClearableFileInput', |
28 | 28 | 'FileInput', 'DateInput', 'DateTimeInput', 'TimeInput', 'Textarea', 'CheckboxInput', |
29 | 29 | 'Select', 'NullBooleanSelect', 'SelectMultiple', 'RadioSelect', |
… |
… |
class TextInput(Input):
|
252 | 252 | super(TextInput, self).__init__(attrs) |
253 | 253 | |
254 | 254 | |
| 255 | class NumberInput(TextInput): |
| 256 | input_type = 'number' |
| 257 | |
| 258 | |
255 | 259 | class EmailInput(TextInput): |
256 | 260 | input_type = 'email' |
257 | 261 | |
diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt
index 2e4e779..85650ad 100644
a
|
b
|
For each field, we describe the default widget used if you don't specify
|
454 | 454 | |
455 | 455 | .. class:: DecimalField(**kwargs) |
456 | 456 | |
457 | | * Default widget: :class:`TextInput` |
| 457 | * Default widget: :class:`NumberInput` when :attr:`Field.localize` is |
| 458 | ``False``, else :class:`TextInput`. |
458 | 459 | * Empty value: ``None`` |
459 | 460 | * Normalizes to: A Python ``decimal``. |
460 | 461 | * Validates that the given value is a decimal. Leading and trailing |
… |
… |
For each field, we describe the default widget used if you don't specify
|
580 | 581 | |
581 | 582 | .. class:: FloatField(**kwargs) |
582 | 583 | |
583 | | * Default widget: :class:`TextInput` |
| 584 | * Default widget: :class:`NumberInput` when :attr:`Field.localize` is |
| 585 | ``False``, else :class:`TextInput`. |
584 | 586 | * Empty value: ``None`` |
585 | 587 | * Normalizes to: A Python float. |
586 | 588 | * Validates that the given value is an float. Leading and trailing |
… |
… |
For each field, we describe the default widget used if you don't specify
|
621 | 623 | |
622 | 624 | .. class:: IntegerField(**kwargs) |
623 | 625 | |
624 | | * Default widget: :class:`TextInput` |
| 626 | * Default widget: :class:`NumberInput` when :attr:`Field.localize` is |
| 627 | ``False``, else :class:`TextInput`. |
625 | 628 | * Empty value: ``None`` |
626 | 629 | * Normalizes to: A Python integer or long integer. |
627 | 630 | * Validates that the given value is an integer. Leading and trailing |
diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt
index cb5224f..fdff0bf 100644
a
|
b
|
These widgets make use of the HTML elements ``input`` and ``textarea``.
|
394 | 394 | |
395 | 395 | Text input: ``<input type="text" ...>`` |
396 | 396 | |
| 397 | ``NumberInput`` |
| 398 | ~~~~~~~~~~~~~~~ |
| 399 | |
| 400 | .. class:: NumberInput |
| 401 | |
| 402 | .. versionadded:: 1.6 |
| 403 | |
| 404 | Text input: ``<input type="number" ...>`` |
| 405 | |
| 406 | Beware that not all browsers support entering localized numbers in |
| 407 | ``number`` input types. Django itself avoids using them for fields having |
| 408 | their :attr:`~django.forms.Field.localize` property to ``True``. |
| 409 | |
397 | 410 | ``EmailInput`` |
398 | 411 | ~~~~~~~~~~~~~~ |
399 | 412 | |
diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt
index 32e5172..dadc8c9 100644
a
|
b
|
Minor features
|
44 | 44 | * Added :meth:`~django.db.models.query.QuerySet.earliest` for symmetry with |
45 | 45 | :meth:`~django.db.models.query.QuerySet.latest`. |
46 | 46 | |
47 | | * The default widgets for :class:`~django.forms.EmailField` and |
48 | | :class:`~django.forms.URLField` use the new type attributes available in |
49 | | HTML5 (type='email', type='url'). |
| 47 | * The default widgets for :class:`~django.forms.EmailField`, |
| 48 | :class:`~django.forms.URLField`, :class:`~django.forms.IntegerField`, |
| 49 | :class:`~django.forms.FloatField` and :class:`~django.forms.DecimalField` use |
| 50 | the new type attributes available in HTML5 (type='email', type='url', |
| 51 | type='number'). Note that due to erratic support of the ``number`` input type |
| 52 | with localized numbers in current browsers, Django only uses it when numeric |
| 53 | fields are not localized. |
50 | 54 | |
51 | 55 | * The ``number`` argument for :ref:`lazy plural translations |
52 | 56 | <lazy-plural-translations>` can be provided at translation time rather than |
… |
… |
Backwards incompatible changes in 1.6
|
71 | 75 | |
72 | 76 | * If your CSS/Javascript code used to access HTML input widgets by type, you |
73 | 77 | should review it as ``type='text'`` widgets might be now output as |
74 | | ``type='email'`` or ``type='url'`` depending on their corresponding field type. |
| 78 | ``type='email'``, ``type='url'`` or ``type='number'`` depending on their |
| 79 | corresponding field type. |
75 | 80 | |
76 | 81 | * Extraction of translatable literals from templates with the |
77 | 82 | :djadmin:`makemessages` command now correctly detects i18n constructs when |
diff --git a/docs/topics/forms/formsets.txt b/docs/topics/forms/formsets.txt
index ee1c69e..b3b5586 100644
a
|
b
|
Lets you create a formset with the ability to order::
|
273 | 273 | ... print(form.as_table()) |
274 | 274 | <tr><th><label for="id_form-0-title">Title:</label></th><td><input type="text" name="form-0-title" value="Article #1" id="id_form-0-title" /></td></tr> |
275 | 275 | <tr><th><label for="id_form-0-pub_date">Pub date:</label></th><td><input type="text" name="form-0-pub_date" value="2008-05-10" id="id_form-0-pub_date" /></td></tr> |
276 | | <tr><th><label for="id_form-0-ORDER">Order:</label></th><td><input type="text" name="form-0-ORDER" value="1" id="id_form-0-ORDER" /></td></tr> |
| 276 | <tr><th><label for="id_form-0-ORDER">Order:</label></th><td><input type="number" name="form-0-ORDER" value="1" id="id_form-0-ORDER" /></td></tr> |
277 | 277 | <tr><th><label for="id_form-1-title">Title:</label></th><td><input type="text" name="form-1-title" value="Article #2" id="id_form-1-title" /></td></tr> |
278 | 278 | <tr><th><label for="id_form-1-pub_date">Pub date:</label></th><td><input type="text" name="form-1-pub_date" value="2008-05-11" id="id_form-1-pub_date" /></td></tr> |
279 | | <tr><th><label for="id_form-1-ORDER">Order:</label></th><td><input type="text" name="form-1-ORDER" value="2" id="id_form-1-ORDER" /></td></tr> |
| 279 | <tr><th><label for="id_form-1-ORDER">Order:</label></th><td><input type="number" name="form-1-ORDER" value="2" id="id_form-1-ORDER" /></td></tr> |
280 | 280 | <tr><th><label for="id_form-2-title">Title:</label></th><td><input type="text" name="form-2-title" id="id_form-2-title" /></td></tr> |
281 | 281 | <tr><th><label for="id_form-2-pub_date">Pub date:</label></th><td><input type="text" name="form-2-pub_date" id="id_form-2-pub_date" /></td></tr> |
282 | | <tr><th><label for="id_form-2-ORDER">Order:</label></th><td><input type="text" name="form-2-ORDER" id="id_form-2-ORDER" /></td></tr> |
| 282 | <tr><th><label for="id_form-2-ORDER">Order:</label></th><td><input type="number" name="form-2-ORDER" id="id_form-2-ORDER" /></td></tr> |
283 | 283 | |
284 | 284 | This adds an additional field to each form. This new field is named ``ORDER`` |
285 | 285 | and is an ``forms.IntegerField``. For the forms that came from the initial |
diff --git a/tests/modeltests/model_forms/tests.py b/tests/modeltests/model_forms/tests.py
index 6ecd127..fb40037 100644
a
|
b
|
class OldFormForXTests(TestCase):
|
1133 | 1133 | <option value="%s">Joe Better</option> |
1134 | 1134 | <option value="%s">Mike Royko</option> |
1135 | 1135 | </select></p> |
1136 | | <p><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></p>''' % (w_woodward.pk, w_bernstein.pk, bw.pk, w_royko.pk)) |
| 1136 | <p><label for="id_age">Age:</label> <input type="number" name="age" id="id_age" min="0" /></p>''' % (w_woodward.pk, w_bernstein.pk, bw.pk, w_royko.pk)) |
1137 | 1137 | |
1138 | 1138 | data = { |
1139 | 1139 | 'writer': six.text_type(w_woodward.pk), |
… |
… |
class OldFormForXTests(TestCase):
|
1151 | 1151 | <option value="%s">Joe Better</option> |
1152 | 1152 | <option value="%s">Mike Royko</option> |
1153 | 1153 | </select></p> |
1154 | | <p><label for="id_age">Age:</label> <input type="text" name="age" value="65" id="id_age" /></p>''' % (w_woodward.pk, w_bernstein.pk, bw.pk, w_royko.pk)) |
| 1154 | <p><label for="id_age">Age:</label> <input type="number" name="age" value="65" id="id_age" min="0" /></p>''' % (w_woodward.pk, w_bernstein.pk, bw.pk, w_royko.pk)) |
1155 | 1155 | |
1156 | 1156 | def test_file_field(self): |
1157 | 1157 | # Test conditions when files is either not given or empty. |
diff --git a/tests/modeltests/model_formsets/tests.py b/tests/modeltests/model_formsets/tests.py
index a028e65..91e1524 100644
a
|
b
|
class ModelFormsetTest(TestCase):
|
384 | 384 | self.assertEqual(len(formset.forms), 1) |
385 | 385 | self.assertHTMLEqual(formset.forms[0].as_p(), |
386 | 386 | '<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" maxlength="100" /></p>\n' |
387 | | '<p><label for="id_form-0-write_speed">Write speed:</label> <input type="text" name="form-0-write_speed" id="id_form-0-write_speed" /><input type="hidden" name="form-0-author_ptr" id="id_form-0-author_ptr" /></p>') |
| 387 | '<p><label for="id_form-0-write_speed">Write speed:</label> <input type="number" name="form-0-write_speed" id="id_form-0-write_speed" /><input type="hidden" name="form-0-author_ptr" id="id_form-0-author_ptr" /></p>') |
388 | 388 | |
389 | 389 | data = { |
390 | 390 | 'form-TOTAL_FORMS': '1', # the number of forms rendered |
… |
… |
class ModelFormsetTest(TestCase):
|
407 | 407 | self.assertEqual(len(formset.forms), 2) |
408 | 408 | self.assertHTMLEqual(formset.forms[0].as_p(), |
409 | 409 | '<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" value="Ernest Hemingway" maxlength="100" /></p>\n' |
410 | | '<p><label for="id_form-0-write_speed">Write speed:</label> <input type="text" name="form-0-write_speed" value="10" id="id_form-0-write_speed" /><input type="hidden" name="form-0-author_ptr" value="%d" id="id_form-0-author_ptr" /></p>' % hemingway_id) |
| 410 | '<p><label for="id_form-0-write_speed">Write speed:</label> <input type="number" name="form-0-write_speed" value="10" id="id_form-0-write_speed" /><input type="hidden" name="form-0-author_ptr" value="%d" id="id_form-0-author_ptr" /></p>' % hemingway_id) |
411 | 411 | self.assertHTMLEqual(formset.forms[1].as_p(), |
412 | 412 | '<p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" maxlength="100" /></p>\n' |
413 | | '<p><label for="id_form-1-write_speed">Write speed:</label> <input type="text" name="form-1-write_speed" id="id_form-1-write_speed" /><input type="hidden" name="form-1-author_ptr" id="id_form-1-author_ptr" /></p>') |
| 413 | '<p><label for="id_form-1-write_speed">Write speed:</label> <input type="number" name="form-1-write_speed" id="id_form-1-write_speed" /><input type="hidden" name="form-1-author_ptr" id="id_form-1-author_ptr" /></p>') |
414 | 414 | |
415 | 415 | data = { |
416 | 416 | 'form-TOTAL_FORMS': '2', # the number of forms rendered |
… |
… |
class ModelFormsetTest(TestCase):
|
543 | 543 | def test_inline_formsets_with_custom_pk(self): |
544 | 544 | # Test inline formsets where the inline-edited object has a custom |
545 | 545 | # primary key that is not the fk to the parent object. |
| 546 | self.maxDiff = 1024 |
546 | 547 | |
547 | 548 | AuthorBooksFormSet2 = inlineformset_factory(Author, BookWithCustomPK, can_delete=False, extra=1) |
548 | 549 | author = Author.objects.create(pk=1, name='Charles Baudelaire') |
… |
… |
class ModelFormsetTest(TestCase):
|
550 | 551 | formset = AuthorBooksFormSet2(instance=author) |
551 | 552 | self.assertEqual(len(formset.forms), 1) |
552 | 553 | self.assertHTMLEqual(formset.forms[0].as_p(), |
553 | | '<p><label for="id_bookwithcustompk_set-0-my_pk">My pk:</label> <input type="text" name="bookwithcustompk_set-0-my_pk" id="id_bookwithcustompk_set-0-my_pk" /></p>\n' |
| 554 | '<p><label for="id_bookwithcustompk_set-0-my_pk">My pk:</label> <input id="id_bookwithcustompk_set-0-my_pk" type="number" name="bookwithcustompk_set-0-my_pk" maxlength="6" /></p>\n' |
554 | 555 | '<p><label for="id_bookwithcustompk_set-0-title">Title:</label> <input id="id_bookwithcustompk_set-0-title" type="text" name="bookwithcustompk_set-0-title" maxlength="100" /><input type="hidden" name="bookwithcustompk_set-0-author" value="1" id="id_bookwithcustompk_set-0-author" /></p>') |
555 | 556 | |
556 | 557 | data = { |
… |
… |
class ModelFormsetTest(TestCase):
|
798 | 799 | '<option value="%d">Joe Perry at Giordanos</option>\n' |
799 | 800 | '<option value="%d">Jack Berry at Giordanos</option>\n' |
800 | 801 | '</select></p>\n' |
801 | | '<p><label for="id_form-0-age">Age:</label> <input type="text" name="form-0-age" id="id_form-0-age" /></p>' |
| 802 | '<p><label for="id_form-0-age">Age:</label> <input type="number" name="form-0-age" id="id_form-0-age" min="0" /></p>' |
802 | 803 | % (owner1.auto_id, owner2.auto_id)) |
803 | 804 | |
804 | 805 | owner1 = Owner.objects.get(name='Joe Perry') |
… |
… |
class ModelFormsetTest(TestCase):
|
808 | 809 | formset = FormSet(instance=owner1) |
809 | 810 | self.assertEqual(len(formset.forms), 1) |
810 | 811 | self.assertHTMLEqual(formset.forms[0].as_p(), |
811 | | '<p><label for="id_ownerprofile-0-age">Age:</label> <input type="text" name="ownerprofile-0-age" id="id_ownerprofile-0-age" /><input type="hidden" name="ownerprofile-0-owner" value="%d" id="id_ownerprofile-0-owner" /></p>' |
| 812 | '<p><label for="id_ownerprofile-0-age">Age:</label> <input type="number" name="ownerprofile-0-age" id="id_ownerprofile-0-age" min="0" /><input type="hidden" name="ownerprofile-0-owner" value="%d" id="id_ownerprofile-0-owner" /></p>' |
812 | 813 | % owner1.auto_id) |
813 | 814 | |
814 | 815 | data = { |
… |
… |
class ModelFormsetTest(TestCase):
|
829 | 830 | formset = FormSet(instance=owner1) |
830 | 831 | self.assertEqual(len(formset.forms), 1) |
831 | 832 | self.assertHTMLEqual(formset.forms[0].as_p(), |
832 | | '<p><label for="id_ownerprofile-0-age">Age:</label> <input type="text" name="ownerprofile-0-age" value="54" id="id_ownerprofile-0-age" /><input type="hidden" name="ownerprofile-0-owner" value="%d" id="id_ownerprofile-0-owner" /></p>' |
| 833 | '<p><label for="id_ownerprofile-0-age">Age:</label> <input type="number" name="ownerprofile-0-age" value="54" id="id_ownerprofile-0-age" min="0" /><input type="hidden" name="ownerprofile-0-owner" value="%d" id="id_ownerprofile-0-owner" /></p>' |
833 | 834 | % owner1.auto_id) |
834 | 835 | |
835 | 836 | data = { |
… |
… |
class ModelFormsetTest(TestCase):
|
985 | 986 | result = re.sub(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)?', '__DATETIME__', result) |
986 | 987 | self.assertHTMLEqual(result, |
987 | 988 | '<p><label for="id_membership_set-0-date_joined">Date joined:</label> <input type="text" name="membership_set-0-date_joined" value="__DATETIME__" id="id_membership_set-0-date_joined" /><input type="hidden" name="initial-membership_set-0-date_joined" value="__DATETIME__" id="initial-membership_set-0-id_membership_set-0-date_joined" /></p>\n' |
988 | | '<p><label for="id_membership_set-0-karma">Karma:</label> <input type="text" name="membership_set-0-karma" id="id_membership_set-0-karma" /><input type="hidden" name="membership_set-0-person" value="%d" id="id_membership_set-0-person" /><input type="hidden" name="membership_set-0-id" id="id_membership_set-0-id" /></p>' |
| 989 | '<p><label for="id_membership_set-0-karma">Karma:</label> <input type="number" name="membership_set-0-karma" id="id_membership_set-0-karma" /><input type="hidden" name="membership_set-0-person" value="%d" id="id_membership_set-0-person" /><input type="hidden" name="membership_set-0-id" id="id_membership_set-0-id" /></p>' |
989 | 990 | % person.id) |
990 | 991 | |
991 | 992 | # test for validation with callable defaults. Validations rely on hidden fields |
diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py
index fc7fc70..3fe2cd2 100644
a
|
b
|
from decimal import Decimal
|
35 | 35 | from django.core.files.uploadedfile import SimpleUploadedFile |
36 | 36 | from django.forms import * |
37 | 37 | from django.test import SimpleTestCase |
38 | | from django.utils import formats |
39 | 38 | from django.utils import six |
40 | 39 | from django.utils._os import upath |
41 | 40 | |
… |
… |
class FieldsTests(SimpleTestCase):
|
131 | 130 | |
132 | 131 | def test_integerfield_1(self): |
133 | 132 | f = IntegerField() |
| 133 | self.assertWidgetRendersTo(f, '<input type="number" name="f" id="id_f" />') |
134 | 134 | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, '') |
135 | 135 | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None) |
136 | 136 | self.assertEqual(1, f.clean('1')) |
… |
… |
class FieldsTests(SimpleTestCase):
|
165 | 165 | |
166 | 166 | def test_integerfield_3(self): |
167 | 167 | f = IntegerField(max_value=10) |
| 168 | self.assertWidgetRendersTo(f, '<input max="10" type="number" name="f" id="id_f" />') |
168 | 169 | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None) |
169 | 170 | self.assertEqual(1, f.clean(1)) |
170 | 171 | self.assertEqual(10, f.clean(10)) |
… |
… |
class FieldsTests(SimpleTestCase):
|
176 | 177 | |
177 | 178 | def test_integerfield_4(self): |
178 | 179 | f = IntegerField(min_value=10) |
| 180 | self.assertWidgetRendersTo(f, '<input id="id_f" type="number" name="f" min="10" />') |
179 | 181 | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None) |
180 | 182 | self.assertRaisesMessage(ValidationError, "'Ensure this value is greater than or equal to 10.'", f.clean, 1) |
181 | 183 | self.assertEqual(10, f.clean(10)) |
… |
… |
class FieldsTests(SimpleTestCase):
|
187 | 189 | |
188 | 190 | def test_integerfield_5(self): |
189 | 191 | f = IntegerField(min_value=10, max_value=20) |
| 192 | self.assertWidgetRendersTo(f, '<input id="id_f" max="20" type="number" name="f" min="10" />') |
190 | 193 | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None) |
191 | 194 | self.assertRaisesMessage(ValidationError, "'Ensure this value is greater than or equal to 10.'", f.clean, 1) |
192 | 195 | self.assertEqual(10, f.clean(10)) |
… |
… |
class FieldsTests(SimpleTestCase):
|
198 | 201 | self.assertEqual(f.max_value, 20) |
199 | 202 | self.assertEqual(f.min_value, 10) |
200 | 203 | |
| 204 | def test_integerfield_localized(self): |
| 205 | """ |
| 206 | Make sure localized IntegerField's widget renders to a text input with |
| 207 | no number input specific attributes. |
| 208 | """ |
| 209 | f1 = IntegerField(localize=True) |
| 210 | self.assertWidgetRendersTo(f1, '<input id="id_f" name="f" type="text" />') |
| 211 | |
201 | 212 | # FloatField ################################################################## |
202 | 213 | |
203 | 214 | def test_floatfield_1(self): |
204 | 215 | f = FloatField() |
| 216 | self.assertWidgetRendersTo(f, '<input step="any" type="number" name="f" id="id_f" />') |
205 | 217 | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, '') |
206 | 218 | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None) |
207 | 219 | self.assertEqual(1.0, f.clean('1')) |
… |
… |
class FieldsTests(SimpleTestCase):
|
228 | 240 | |
229 | 241 | def test_floatfield_3(self): |
230 | 242 | f = FloatField(max_value=1.5, min_value=0.5) |
| 243 | self.assertWidgetRendersTo(f, '<input step="any" name="f" min="0.5" max="1.5" type="number" id="id_f" />') |
231 | 244 | self.assertRaisesMessage(ValidationError, "'Ensure this value is less than or equal to 1.5.'", f.clean, '1.6') |
232 | 245 | self.assertRaisesMessage(ValidationError, "'Ensure this value is greater than or equal to 0.5.'", f.clean, '0.4') |
233 | 246 | self.assertEqual(1.5, f.clean('1.5')) |
… |
… |
class FieldsTests(SimpleTestCase):
|
235 | 248 | self.assertEqual(f.max_value, 1.5) |
236 | 249 | self.assertEqual(f.min_value, 0.5) |
237 | 250 | |
| 251 | def test_floatfield_localized(self): |
| 252 | """ |
| 253 | Make sure localized FloatField's widget renders to a text input with |
| 254 | no number input specific attributes. |
| 255 | """ |
| 256 | f = FloatField(localize=True) |
| 257 | self.assertWidgetRendersTo(f, '<input id="id_f" name="f" type="text" />') |
| 258 | |
238 | 259 | # DecimalField ################################################################ |
239 | 260 | |
240 | 261 | def test_decimalfield_1(self): |
241 | 262 | f = DecimalField(max_digits=4, decimal_places=2) |
| 263 | self.assertWidgetRendersTo(f, '<input id="id_f" step="0.01" type="number" name="f" maxlength="6" />') |
242 | 264 | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, '') |
243 | 265 | self.assertRaisesMessage(ValidationError, "'This field is required.'", f.clean, None) |
244 | 266 | self.assertEqual(f.clean('1'), Decimal("1")) |
… |
… |
class FieldsTests(SimpleTestCase):
|
284 | 306 | |
285 | 307 | def test_decimalfield_3(self): |
286 | 308 | f = DecimalField(max_digits=4, decimal_places=2, max_value=Decimal('1.5'), min_value=Decimal('0.5')) |
| 309 | self.assertWidgetRendersTo(f, '<input step="0.01" name="f" min="0.5" max="1.5" maxlength="6" type="number" id="id_f" />') |
287 | 310 | self.assertRaisesMessage(ValidationError, "'Ensure this value is less than or equal to 1.5.'", f.clean, '1.6') |
288 | 311 | self.assertRaisesMessage(ValidationError, "'Ensure this value is greater than or equal to 0.5.'", f.clean, '0.4') |
289 | 312 | self.assertEqual(f.clean('1.5'), Decimal("1.5")) |
… |
… |
class FieldsTests(SimpleTestCase):
|
315 | 338 | self.assertEqual(f.clean('.01'), Decimal(".01")) |
316 | 339 | self.assertRaisesMessage(ValidationError, "'Ensure that there are no more than 0 digits before the decimal point.'", f.clean, '1.1') |
317 | 340 | |
| 341 | def test_decimalfield_localized(self): |
| 342 | """ |
| 343 | Make sure localized DecimalField's widget renders to a text input with |
| 344 | no number input specific attributes. |
| 345 | """ |
| 346 | f = DecimalField(localize=True) |
| 347 | self.assertWidgetRendersTo(f, '<input id="id_f" name="f" type="text" />') |
| 348 | |
318 | 349 | # DateField ################################################################### |
319 | 350 | |
320 | 351 | def test_datefield_1(self): |
diff --git a/tests/regressiontests/forms/tests/forms.py b/tests/regressiontests/forms/tests/forms.py
index f2fa78e..f856e30 100644
a
|
b
|
class FormsTestCase(TestCase):
|
1740 | 1740 | <option value="3">No</option> |
1741 | 1741 | </select></li> |
1742 | 1742 | <li><label for="id_email">Email:</label> <input type="email" name="email" id="id_email" /></li> |
1743 | | <li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></li>""") |
| 1743 | <li class="required error"><ul class="errorlist"><li>This field is required.</li></ul><label for="id_age">Age:</label> <input type="number" name="age" id="id_age" /></li>""") |
1744 | 1744 | |
1745 | 1745 | self.assertHTMLEqual(p.as_p(), """<ul class="errorlist"><li>This field is required.</li></ul> |
1746 | 1746 | <p class="required error"><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p> |
… |
… |
class FormsTestCase(TestCase):
|
1751 | 1751 | </select></p> |
1752 | 1752 | <p><label for="id_email">Email:</label> <input type="email" name="email" id="id_email" /></p> |
1753 | 1753 | <ul class="errorlist"><li>This field is required.</li></ul> |
1754 | | <p class="required error"><label for="id_age">Age:</label> <input type="text" name="age" id="id_age" /></p>""") |
| 1754 | <p class="required error"><label for="id_age">Age:</label> <input type="number" name="age" id="id_age" /></p>""") |
1755 | 1755 | |
1756 | 1756 | self.assertHTMLEqual(p.as_table(), """<tr class="required error"><th><label for="id_name">Name:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="name" id="id_name" /></td></tr> |
1757 | 1757 | <tr class="required"><th><label for="id_is_cool">Is cool:</label></th><td><select name="is_cool" id="id_is_cool"> |
… |
… |
class FormsTestCase(TestCase):
|
1760 | 1760 | <option value="3">No</option> |
1761 | 1761 | </select></td></tr> |
1762 | 1762 | <tr><th><label for="id_email">Email:</label></th><td><input type="email" name="email" id="id_email" /></td></tr> |
1763 | | <tr class="required error"><th><label for="id_age">Age:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="text" name="age" id="id_age" /></td></tr>""") |
| 1763 | <tr class="required error"><th><label for="id_age">Age:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input type="number" name="age" id="id_age" /></td></tr>""") |
1764 | 1764 | |
1765 | 1765 | def test_label_split_datetime_not_displayed(self): |
1766 | 1766 | class EventForm(Form): |
diff --git a/tests/regressiontests/forms/tests/formsets.py b/tests/regressiontests/forms/tests/formsets.py
index ef6f40c..d08110f 100644
a
|
b
|
class FormsFormsetTestCase(TestCase):
|
53 | 53 | formset = ChoiceFormSet(auto_id=False, prefix='choices') |
54 | 54 | self.assertHTMLEqual(str(formset), """<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_NUM_FORMS" /> |
55 | 55 | <tr><th>Choice:</th><td><input type="text" name="choices-0-choice" /></td></tr> |
56 | | <tr><th>Votes:</th><td><input type="text" name="choices-0-votes" /></td></tr>""") |
| 56 | <tr><th>Votes:</th><td><input type="number" name="choices-0-votes" /></td></tr>""") |
57 | 57 | |
58 | 58 | # On thing to note is that there needs to be a special value in the data. This |
59 | 59 | # value tells the FormSet how many forms were displayed so it can tell how |
… |
… |
class FormsFormsetTestCase(TestCase):
|
137 | 137 | form_output.append(form.as_ul()) |
138 | 138 | |
139 | 139 | self.assertHTMLEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li> |
140 | | <li>Votes: <input type="text" name="choices-0-votes" value="100" /></li> |
| 140 | <li>Votes: <input type="number" name="choices-0-votes" value="100" /></li> |
141 | 141 | <li>Choice: <input type="text" name="choices-1-choice" /></li> |
142 | | <li>Votes: <input type="text" name="choices-1-votes" /></li>""") |
| 142 | <li>Votes: <input type="number" name="choices-1-votes" /></li>""") |
143 | 143 | |
144 | 144 | # Let's simulate what would happen if we submitted this form. |
145 | 145 | |
… |
… |
class FormsFormsetTestCase(TestCase):
|
210 | 210 | form_output.append(form.as_ul()) |
211 | 211 | |
212 | 212 | self.assertHTMLEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" /></li> |
213 | | <li>Votes: <input type="text" name="choices-0-votes" /></li> |
| 213 | <li>Votes: <input type="number" name="choices-0-votes" /></li> |
214 | 214 | <li>Choice: <input type="text" name="choices-1-choice" /></li> |
215 | | <li>Votes: <input type="text" name="choices-1-votes" /></li> |
| 215 | <li>Votes: <input type="number" name="choices-1-votes" /></li> |
216 | 216 | <li>Choice: <input type="text" name="choices-2-choice" /></li> |
217 | | <li>Votes: <input type="text" name="choices-2-votes" /></li>""") |
| 217 | <li>Votes: <input type="number" name="choices-2-votes" /></li>""") |
218 | 218 | |
219 | 219 | # Since we displayed every form as blank, we will also accept them back as blank. |
220 | 220 | # This may seem a little strange, but later we will show how to require a minimum |
… |
… |
class FormsFormsetTestCase(TestCase):
|
301 | 301 | form_output.append(form.as_ul()) |
302 | 302 | |
303 | 303 | self.assertHTMLEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li> |
304 | | <li>Votes: <input type="text" name="choices-0-votes" value="100" /></li> |
| 304 | <li>Votes: <input type="number" name="choices-0-votes" value="100" /></li> |
305 | 305 | <li>Choice: <input type="text" name="choices-1-choice" /></li> |
306 | | <li>Votes: <input type="text" name="choices-1-votes" /></li> |
| 306 | <li>Votes: <input type="number" name="choices-1-votes" /></li> |
307 | 307 | <li>Choice: <input type="text" name="choices-2-choice" /></li> |
308 | | <li>Votes: <input type="text" name="choices-2-votes" /></li> |
| 308 | <li>Votes: <input type="number" name="choices-2-votes" /></li> |
309 | 309 | <li>Choice: <input type="text" name="choices-3-choice" /></li> |
310 | | <li>Votes: <input type="text" name="choices-3-votes" /></li>""") |
| 310 | <li>Votes: <input type="number" name="choices-3-votes" /></li>""") |
311 | 311 | |
312 | 312 | # Make sure retrieving an empty form works, and it shows up in the form list |
313 | 313 | |
314 | 314 | self.assertTrue(formset.empty_form.empty_permitted) |
315 | 315 | self.assertHTMLEqual(formset.empty_form.as_ul(), """<li>Choice: <input type="text" name="choices-__prefix__-choice" /></li> |
316 | | <li>Votes: <input type="text" name="choices-__prefix__-votes" /></li>""") |
| 316 | <li>Votes: <input type="number" name="choices-__prefix__-votes" /></li>""") |
317 | 317 | |
318 | 318 | def test_formset_with_deletion(self): |
319 | 319 | # FormSets with deletion ###################################################### |
… |
… |
class FormsFormsetTestCase(TestCase):
|
331 | 331 | form_output.append(form.as_ul()) |
332 | 332 | |
333 | 333 | self.assertHTMLEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li> |
334 | | <li>Votes: <input type="text" name="choices-0-votes" value="100" /></li> |
| 334 | <li>Votes: <input type="number" name="choices-0-votes" value="100" /></li> |
335 | 335 | <li>Delete: <input type="checkbox" name="choices-0-DELETE" /></li> |
336 | 336 | <li>Choice: <input type="text" name="choices-1-choice" value="Fergie" /></li> |
337 | | <li>Votes: <input type="text" name="choices-1-votes" value="900" /></li> |
| 337 | <li>Votes: <input type="number" name="choices-1-votes" value="900" /></li> |
338 | 338 | <li>Delete: <input type="checkbox" name="choices-1-DELETE" /></li> |
339 | 339 | <li>Choice: <input type="text" name="choices-2-choice" /></li> |
340 | | <li>Votes: <input type="text" name="choices-2-votes" /></li> |
| 340 | <li>Votes: <input type="number" name="choices-2-votes" /></li> |
341 | 341 | <li>Delete: <input type="checkbox" name="choices-2-DELETE" /></li>""") |
342 | 342 | |
343 | 343 | # To delete something, we just need to set that form's special delete field to |
… |
… |
class FormsFormsetTestCase(TestCase):
|
428 | 428 | form_output.append(form.as_ul()) |
429 | 429 | |
430 | 430 | self.assertHTMLEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li> |
431 | | <li>Votes: <input type="text" name="choices-0-votes" value="100" /></li> |
432 | | <li>Order: <input type="text" name="choices-0-ORDER" value="1" /></li> |
| 431 | <li>Votes: <input type="number" name="choices-0-votes" value="100" /></li> |
| 432 | <li>Order: <input type="number" name="choices-0-ORDER" value="1" /></li> |
433 | 433 | <li>Choice: <input type="text" name="choices-1-choice" value="Fergie" /></li> |
434 | | <li>Votes: <input type="text" name="choices-1-votes" value="900" /></li> |
435 | | <li>Order: <input type="text" name="choices-1-ORDER" value="2" /></li> |
| 434 | <li>Votes: <input type="number" name="choices-1-votes" value="900" /></li> |
| 435 | <li>Order: <input type="number" name="choices-1-ORDER" value="2" /></li> |
436 | 436 | <li>Choice: <input type="text" name="choices-2-choice" /></li> |
437 | | <li>Votes: <input type="text" name="choices-2-votes" /></li> |
438 | | <li>Order: <input type="text" name="choices-2-ORDER" /></li>""") |
| 437 | <li>Votes: <input type="number" name="choices-2-votes" /></li> |
| 438 | <li>Order: <input type="number" name="choices-2-ORDER" /></li>""") |
439 | 439 | |
440 | 440 | data = { |
441 | 441 | 'choices-TOTAL_FORMS': '3', # the number of forms rendered |
… |
… |
class FormsFormsetTestCase(TestCase):
|
539 | 539 | form_output.append(form.as_ul()) |
540 | 540 | |
541 | 541 | self.assertHTMLEqual('\n'.join(form_output), """<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li> |
542 | | <li>Votes: <input type="text" name="choices-0-votes" value="100" /></li> |
543 | | <li>Order: <input type="text" name="choices-0-ORDER" value="1" /></li> |
| 542 | <li>Votes: <input type="number" name="choices-0-votes" value="100" /></li> |
| 543 | <li>Order: <input type="number" name="choices-0-ORDER" value="1" /></li> |
544 | 544 | <li>Delete: <input type="checkbox" name="choices-0-DELETE" /></li> |
545 | 545 | <li>Choice: <input type="text" name="choices-1-choice" value="Fergie" /></li> |
546 | | <li>Votes: <input type="text" name="choices-1-votes" value="900" /></li> |
547 | | <li>Order: <input type="text" name="choices-1-ORDER" value="2" /></li> |
| 546 | <li>Votes: <input type="number" name="choices-1-votes" value="900" /></li> |
| 547 | <li>Order: <input type="number" name="choices-1-ORDER" value="2" /></li> |
548 | 548 | <li>Delete: <input type="checkbox" name="choices-1-DELETE" /></li> |
549 | 549 | <li>Choice: <input type="text" name="choices-2-choice" value="The Decemberists" /></li> |
550 | | <li>Votes: <input type="text" name="choices-2-votes" value="500" /></li> |
551 | | <li>Order: <input type="text" name="choices-2-ORDER" value="3" /></li> |
| 550 | <li>Votes: <input type="number" name="choices-2-votes" value="500" /></li> |
| 551 | <li>Order: <input type="number" name="choices-2-ORDER" value="3" /></li> |
552 | 552 | <li>Delete: <input type="checkbox" name="choices-2-DELETE" /></li> |
553 | 553 | <li>Choice: <input type="text" name="choices-3-choice" /></li> |
554 | | <li>Votes: <input type="text" name="choices-3-votes" /></li> |
555 | | <li>Order: <input type="text" name="choices-3-ORDER" /></li> |
| 554 | <li>Votes: <input type="number" name="choices-3-votes" /></li> |
| 555 | <li>Order: <input type="number" name="choices-3-ORDER" /></li> |
556 | 556 | <li>Delete: <input type="checkbox" name="choices-3-DELETE" /></li>""") |
557 | 557 | |
558 | 558 | # Let's delete Fergie, and put The Decemberists ahead of Calexico. |
… |
… |
class FormsetAsFooTests(TestCase):
|
898 | 898 | formset = ChoiceFormSet(data, auto_id=False, prefix='choices') |
899 | 899 | self.assertHTMLEqual(formset.as_table(),"""<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_NUM_FORMS" value="0" /> |
900 | 900 | <tr><th>Choice:</th><td><input type="text" name="choices-0-choice" value="Calexico" /></td></tr> |
901 | | <tr><th>Votes:</th><td><input type="text" name="choices-0-votes" value="100" /></td></tr>""") |
| 901 | <tr><th>Votes:</th><td><input type="number" name="choices-0-votes" value="100" /></td></tr>""") |
902 | 902 | |
903 | 903 | def test_as_p(self): |
904 | 904 | formset = ChoiceFormSet(data, auto_id=False, prefix='choices') |
905 | 905 | self.assertHTMLEqual(formset.as_p(),"""<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_NUM_FORMS" value="0" /> |
906 | 906 | <p>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></p> |
907 | | <p>Votes: <input type="text" name="choices-0-votes" value="100" /></p>""") |
| 907 | <p>Votes: <input type="number" name="choices-0-votes" value="100" /></p>""") |
908 | 908 | |
909 | 909 | def test_as_ul(self): |
910 | 910 | formset = ChoiceFormSet(data, auto_id=False, prefix='choices') |
911 | 911 | self.assertHTMLEqual(formset.as_ul(),"""<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_NUM_FORMS" value="0" /> |
912 | 912 | <li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li> |
913 | | <li>Votes: <input type="text" name="choices-0-votes" value="100" /></li>""") |
| 913 | <li>Votes: <input type="number" name="choices-0-votes" value="100" /></li>""") |
914 | 914 | |
915 | 915 | |
916 | 916 | # Regression test for #11418 ################################################# |
diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
index 7b4fd0e..ca34007 100644
a
|
b
|
class FormattingTests(TestCase):
|
662 | 662 | """ |
663 | 663 | Tests if form input is correctly localized |
664 | 664 | """ |
| 665 | self.maxDiff = 1200 |
665 | 666 | settings.USE_L10N = True |
666 | 667 | with translation.override('de-at', deactivate=True): |
667 | 668 | form6 = CompanyForm({ |