diff --git a/django/forms/fields.py b/django/forms/fields.py index 4f17e42278..ff71328f52 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -532,9 +532,8 @@ class EmailField(CharField): widget = EmailInput default_validators = [validators.validate_email] - def clean(self, value): - value = self.to_python(value).strip() - return super(EmailField, self).clean(value) + def __init__(self, *args, **kwargs): + super(EmailField, self).__init__(*args, strip=True, **kwargs) class FileField(Field): @@ -665,6 +664,9 @@ class URLField(CharField): } default_validators = [validators.URLValidator()] + def __init__(self, *args, **kwargs): + super(URLField, self).__init__(*args, strip=True, **kwargs) + def to_python(self, value): def split_url(url): @@ -696,10 +698,6 @@ class URLField(CharField): value = urlunsplit(url_fields) return value - def clean(self, value): - value = self.to_python(value).strip() - return super(URLField, self).clean(value) - class BooleanField(Field): widget = CheckboxInput diff --git a/docs/releases/1.11.txt b/docs/releases/1.11.txt index 0ed83a1da8..dc01a824e8 100644 --- a/docs/releases/1.11.txt +++ b/docs/releases/1.11.txt @@ -334,6 +334,10 @@ Miscellaneous new :meth:`.AbstractUser.clean` method so that normalization is applied in cases like model form validation. +* ``EmailField`` and ``URLField`` no longer accept the ``strip`` keyword + argument. Remove it because it doesn't have an effect in older versions of + Django as these fields alway strip whitespace. + .. _deprecated-features-1.11: Features deprecated in 1.11 diff --git a/tests/forms_tests/field_tests/test_emailfield.py b/tests/forms_tests/field_tests/test_emailfield.py index 4d37a7dc00..906a6cf5ff 100644 --- a/tests/forms_tests/field_tests/test_emailfield.py +++ b/tests/forms_tests/field_tests/test_emailfield.py @@ -51,3 +51,12 @@ class EmailFieldTest(FormFieldAssertionsMixin, SimpleTestCase): self.assertEqual('alf@foo.com', f.clean('alf@foo.com')) with self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 15 characters (it has 20).'"): f.clean('alf123456788@foo.com') + + def test_emailfield_strip_on_none_value(self): + f = EmailField(required=False, empty_value=None) + self.assertIsNone(f.clean(None)) + + def test_emailfield_unable_to_set_strip_kwarg(self): + msg = "__init__() got multiple values for keyword argument 'strip'" + with self.assertRaisesMessage(TypeError, msg): + EmailField(strip=False) diff --git a/tests/forms_tests/field_tests/test_urlfield.py b/tests/forms_tests/field_tests/test_urlfield.py index 24da712717..d0b6d00482 100644 --- a/tests/forms_tests/field_tests/test_urlfield.py +++ b/tests/forms_tests/field_tests/test_urlfield.py @@ -151,3 +151,12 @@ class URLFieldTest(FormFieldAssertionsMixin, SimpleTestCase): def test_urlfield_normalization(self): f = URLField() self.assertEqual(f.clean('http://example.com/ '), 'http://example.com/') + + def test_urlfield_strip_on_none_value(self): + f = URLField(required=False, empty_value=None) + self.assertIsNone(f.clean(None)) + + def test_urlfield_unable_to_set_strip_kwarg(self): + msg = "__init__() got multiple values for keyword argument 'strip'" + with self.assertRaisesMessage(TypeError, msg): + URLField(strip=False)