8000 Fix #18761: Added whitespace stripping to URLField and SlugField by denibertovic · Pull Request #1113 · django/django · GitHub
[go: up one dir, main page]

Skip to content

Fix #18761: Added whitespace stripping to URLField and SlugField #1113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions django/forms/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 changes: 8 additions & 0 deletions tests/forms_tests/tests/test_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
0