Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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
Honza Král authored September 11, 2009
2  django/core/validators.py
@@ -44,7 +44,7 @@ class URLValidator(RegexValidator):
44 44
         r'localhost|' #localhost...
45 45
         r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
46 46
         r'(?::\d+)?' # optional port
47  
-        r'(?:/?|/\S+)$', re.IGNORECASE)
  47
+        r'(?:/?|[/?]\S+)$', re.IGNORECASE)
48 48
 
49 49
     def __init__(self, verify_exists=False, validator_user_agent=URL_VALIDATOR_USER_AGENT):
50 50
         super(URLValidator, self).__init__()
15  django/forms/fields.py
@@ -551,12 +551,15 @@ def __init__(self, max_length=None, min_length=None, verify_exists=False,
551 551
         self.validators.append(validators.URLValidator(verify_exists=verify_exists, validator_user_agent=validator_user_agent))
552 552
 
553 553
     def to_python(self, value):
554  
-        # If no URL scheme given, assume http://
555  
-        if value and '://' not in value:
556  
-            value = u'http://%s' % value
557  
-        # If no URL path given, assume /
558  
-        if value and not urlparse.urlsplit(value)[2]:
559  
-            value += '/'
  554
+        if value: 
  555
+            if '://' not in value: 
  556
+                # If no URL scheme given, assume http:// 
  557
+                value = u'http://%s' % value 
  558
+            url_fields = list(urlparse.urlsplit(value)) 
  559
+            if not url_fields[2]: 
  560
+                # the path portion may need to be added before query params 
  561
+                url_fields[2] = '/' 
  562
+                value = urlparse.urlunsplit(url_fields) 
560 563
         return super(URLField, self).to_python(value)
561 564
 
562 565
 class BooleanField(Field):
2  tests/modeltests/validators/tests.py
@@ -106,6 +106,8 @@ class TestSimpleValidators(TestCase):
106 106
     (URLValidator(), 'http://200.8.9.10/', None),
107 107
     (URLValidator(), 'http://200.8.9.10:8000/test', None),
108 108
     (URLValidator(), 'http://valid-----hyphens.com/', None),
  109
+    (URLValidator(), 'http://example.com?something=value', None),
  110
+    (URLValidator(), 'http://example.com/index.php?something=value&another=value2', None),
109 111
 
110 112
     (URLValidator(), 'foo', ValidationError),
111 113
     (URLValidator(), 'http://', ValidationError),
4  tests/regressiontests/forms/fields.py
@@ -496,6 +496,10 @@ def test_urlfield_43(self):
496 496
         f = URLField()
497 497
         self.assertEqual(u'http://example.com/', f.clean('http://example.com'))
498 498
         self.assertEqual(u'http://example.com/test', f.clean('http://example.com/test'))
  499
+
  500
+    def test_urlfield_ticket11826(self):
  501
+        f = URLField()
  502
+        self.assertEqual(u'http://example.com/?some_param=some_value', f.clean('http://example.com?some_param=some_value'))
499 503
         
500 504
     # BooleanField ################################################################
501 505
 

0 notes on commit 83a3588

Please sign in to comment.
Something went wrong with that request. Please try again.