Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[soc2009/model-validation] Fixed #11826 django.forms.fields.URLField …

…rejects valid URLs with no abs_path component

Thanks wam

git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2009/model-validation@11525 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 83a3588ff712d5fe44e9692f5cb6a1d020f3ab2f 1 parent 9222091
@HonzaKral HonzaKral authored
View
2  django/core/validators.py
@@ -44,7 +44,7 @@ class URLValidator(RegexValidator):
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
- r'(?:/?|/\S+)$', re.IGNORECASE)
+ r'(?:/?|[/?]\S+)$', re.IGNORECASE)
def __init__(self, verify_exists=False, validator_user_agent=URL_VALIDATOR_USER_AGENT):
super(URLValidator, self).__init__()
View
15 django/forms/fields.py
@@ -551,12 +551,15 @@ def __init__(self, max_length=None, min_length=None, verify_exists=False,
self.validators.append(validators.URLValidator(verify_exists=verify_exists, validator_user_agent=validator_user_agent))
def to_python(self, value):
- # If no URL scheme given, assume http://
- if value and '://' not in value:
- value = u'http://%s' % value
- # If no URL path given, assume /
- if value and not urlparse.urlsplit(value)[2]:
- value += '/'
+ if value:
+ if '://' not in value:
+ # If no URL scheme given, assume http://
+ value = u'http://%s' % value
+ url_fields = list(urlparse.urlsplit(value))
+ if not url_fields[2]:
+ # the path portion may need to be added before query params
+ url_fields[2] = '/'
+ value = urlparse.urlunsplit(url_fields)
return super(URLField, self).to_python(value)
class BooleanField(Field):
View
2  tests/modeltests/validators/tests.py
@@ -106,6 +106,8 @@ class TestSimpleValidators(TestCase):
(URLValidator(), 'http://200.8.9.10/', None),
(URLValidator(), 'http://200.8.9.10:8000/test', None),
(URLValidator(), 'http://valid-----hyphens.com/', None),
+ (URLValidator(), 'http://example.com?something=value', None),
+ (URLValidator(), 'http://example.com/index.php?something=value&another=value2', None),
(URLValidator(), 'foo', ValidationError),
(URLValidator(), 'http://', ValidationError),
View
4 tests/regressiontests/forms/fields.py
@@ -496,6 +496,10 @@ def test_urlfield_43(self):
f = URLField()
self.assertEqual(u'http://example.com/', f.clean('http://example.com'))
self.assertEqual(u'http://example.com/test', f.clean('http://example.com/test'))
+
+ def test_urlfield_ticket11826(self):
+ f = URLField()
+ self.assertEqual(u'http://example.com/?some_param=some_value', f.clean('http://example.com?some_param=some_value'))
# BooleanField ################################################################
Please sign in to comment.
Something went wrong with that request. Please try again.