From 186ec21a3dd8c87704c579b6620da931e0f68bb3 Mon Sep 17 00:00:00 2001 From: Deni Bertovic Date: Sat, 18 May 2013 16:06:08 +0200 Subject: [PATCH] Added stripping of whitespace for SlugField and URLField --- django/forms/fields.py | 8 ++++++++ tests/forms_tests/tests/test_extra.py | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/django/forms/fields.py b/django/forms/fields.py index 4ce57d34a3..3ef0d72463 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -670,6 +670,10 @@ 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 @@ -1105,3 +1109,7 @@ class GenericIPAddressField(CharField): class SlugField(CharField): default_validators = [validators.validate_slug] + + def clean(self, value): + value = self.to_python(value).strip() + return super(SlugField, self).clean(value) diff --git a/tests/forms_tests/tests/test_extra.py b/tests/forms_tests/tests/test_extra.py index a83cdfc05f..ea0f063c30 100644 --- a/tests/forms_tests/tests/test_extra.py +++ b/tests/forms_tests/tests/test_extra.py @@ -569,6 +569,14 @@ class FormsExtraTestCase(TestCase, AssertFormErrorsMixin): f = GenericIPAddressField(unpack_ipv4=True) self.assertEqual(f.clean(' ::ffff:0a0a:0a0a'), '10.10.10.10') + def test_slugfield_normalization(self): + f = SlugField() + self.assertEqual(f.clean(' aa-bb-cc '), 'aa-bb-cc') + + def test_urlfield_normalization(self): + f = URLField() + self.assertEqual(f.clean('http://example.com/ '), 'http://example.com/') + def test_smart_text(self): class Test: if six.PY3: