Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added stripping of whitespace for SlugField and URLField

  • Loading branch information...
commit 186ec21a3dd8c87704c579b6620da931e0f68bb3 1 parent f54a888
Deni Bertovic denibertovic authored
Showing with 16 additions and 0 deletions.
  1. +8 −0 django/forms/fields.py
  2. +8 −0 tests/forms_tests/tests/test_extra.py
8 django/forms/fields.py
View
@@ -670,6 +670,10 @@ def split_url(url):
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 @@ def to_python(self, value):
class SlugField(CharField):
default_validators = [validators.validate_slug]
+
+ def clean(self, value):
+ value = self.to_python(value).strip()
+ return super(SlugField, self).clean(value)
8 tests/forms_tests/tests/test_extra.py
View
@@ -569,6 +569,14 @@ def test_generic_ipaddress_normalization(self):
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:
Please sign in to comment.
Something went wrong with that request. Please try again.