Fixed #7345 -- When normalising the URLField form field, attach a trailing

slash when only a host (no path) is given. Thanks, jpwatts.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8089 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2008-07-26 05:18:39 +00:00
parent badde8a7e5
commit 108b604b51
2 changed files with 26 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import datetime
import os import os
import re import re
import time import time
import urlparse
try: try:
from cStringIO import StringIO from cStringIO import StringIO
except ImportError: except ImportError:
@ -539,6 +540,9 @@ class URLField(RegexField):
# If no URL scheme given, assume http:// # If no URL scheme given, assume http://
if value and '://' not in value: if value and '://' not in value:
value = u'http://%s' % value value = u'http://%s' % value
# If no URL path given, assume /
if value and not urlparse.urlsplit(value).path:
value += '/'
value = super(URLField, self).clean(value) value = super(URLField, self).clean(value)
if value == u'': if value == u'':
return value return value

View File

@ -820,15 +820,15 @@ Traceback (most recent call last):
... ...
ValidationError: [u'This field is required.'] ValidationError: [u'This field is required.']
>>> f.clean('http://localhost') >>> f.clean('http://localhost')
u'http://localhost' u'http://localhost/'
>>> f.clean('http://example.com') >>> f.clean('http://example.com')
u'http://example.com' u'http://example.com/'
>>> f.clean('http://www.example.com') >>> f.clean('http://www.example.com')
u'http://www.example.com' u'http://www.example.com/'
>>> f.clean('http://www.example.com:8000/test') >>> f.clean('http://www.example.com:8000/test')
u'http://www.example.com:8000/test' u'http://www.example.com:8000/test'
>>> f.clean('http://200.8.9.10') >>> f.clean('http://200.8.9.10')
u'http://200.8.9.10' u'http://200.8.9.10/'
>>> f.clean('http://200.8.9.10:8000/test') >>> f.clean('http://200.8.9.10:8000/test')
u'http://200.8.9.10:8000/test' u'http://200.8.9.10:8000/test'
>>> f.clean('foo') >>> f.clean('foo')
@ -858,9 +858,9 @@ u''
>>> f.clean(None) >>> f.clean(None)
u'' u''
>>> f.clean('http://example.com') >>> f.clean('http://example.com')
u'http://example.com' u'http://example.com/'
>>> f.clean('http://www.example.com') >>> f.clean('http://www.example.com')
u'http://www.example.com' u'http://www.example.com/'
>>> f.clean('foo') >>> f.clean('foo')
Traceback (most recent call last): Traceback (most recent call last):
... ...
@ -886,7 +886,7 @@ URLField takes an optional verify_exists parameter, which is False by default.
This verifies that the URL is live on the Internet and doesn't return a 404 or 500: This verifies that the URL is live on the Internet and doesn't return a 404 or 500:
>>> f = URLField(verify_exists=True) >>> f = URLField(verify_exists=True)
>>> f.clean('http://www.google.com') # This will fail if there's no Internet connection >>> f.clean('http://www.google.com') # This will fail if there's no Internet connection
u'http://www.google.com' u'http://www.google.com/'
>>> f.clean('http://example') >>> f.clean('http://example')
Traceback (most recent call last): Traceback (most recent call last):
... ...
@ -903,29 +903,38 @@ ValidationError: [u'This URL appears to be a broken link.']
>>> f.clean('') >>> f.clean('')
u'' u''
>>> f.clean('http://www.google.com') # This will fail if there's no Internet connection >>> f.clean('http://www.google.com') # This will fail if there's no Internet connection
u'http://www.google.com' u'http://www.google.com/'
URLField also access min_length and max_length parameters, for convenience. URLField also access min_length and max_length parameters, for convenience.
>>> f = URLField(min_length=15, max_length=20) >>> f = URLField(min_length=15, max_length=20)
>>> f.clean('http://f.com') >>> f.clean('http://f.com')
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValidationError: [u'Ensure this value has at least 15 characters (it has 12).'] ValidationError: [u'Ensure this value has at least 15 characters (it has 13).']
>>> f.clean('http://example.com') >>> f.clean('http://example.com')
u'http://example.com' u'http://example.com/'
>>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com') >>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com')
Traceback (most recent call last): Traceback (most recent call last):
... ...
ValidationError: [u'Ensure this value has at most 20 characters (it has 37).'] ValidationError: [u'Ensure this value has at most 20 characters (it has 38).']
URLField should prepend 'http://' if no scheme was given URLField should prepend 'http://' if no scheme was given
>>> f = URLField(required=False) >>> f = URLField(required=False)
>>> f.clean('example.com') >>> f.clean('example.com')
u'http://example.com' u'http://example.com/'
>>> f.clean('') >>> f.clean('')
u'' u''
>>> f.clean('https://example.com') >>> f.clean('https://example.com')
u'https://example.com' u'https://example.com/'
URLField should append '/' if no path was given
>>> f = URLField()
>>> f.clean('http://example.com')
u'http://example.com/'
URLField shouldn't change the path if it was given
>>> f.clean('http://example.com/test')
u'http://example.com/test'
# BooleanField ################################################################ # BooleanField ################################################################