Added stripping of whitespace for SlugField and URLField

This commit is contained in:
Deni Bertovic 2013-05-18 16:06:08 +02:00
parent f54a8880d7
commit 186ec21a3d
2 changed files with 16 additions and 0 deletions

View File

@ -670,6 +670,10 @@ class URLField(CharField):
value = urlunsplit(url_fields) value = urlunsplit(url_fields)
return value return value
def clean(self, value):
value = self.to_python(value).strip()
return super(URLField, self).clean(value)
class BooleanField(Field): class BooleanField(Field):
widget = CheckboxInput widget = CheckboxInput
@ -1105,3 +1109,7 @@ class GenericIPAddressField(CharField):
class SlugField(CharField): class SlugField(CharField):
default_validators = [validators.validate_slug] default_validators = [validators.validate_slug]
def clean(self, value):
value = self.to_python(value).strip()
return super(SlugField, self).clean(value)

View File

@ -569,6 +569,14 @@ class FormsExtraTestCase(TestCase, AssertFormErrorsMixin):
f = GenericIPAddressField(unpack_ipv4=True) f = GenericIPAddressField(unpack_ipv4=True)
self.assertEqual(f.clean(' ::ffff:0a0a:0a0a'), '10.10.10.10') 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): def test_smart_text(self):
class Test: class Test:
if six.PY3: if six.PY3: