Ticket #16664: url_field.patch

File url_field.patch, 2.2 KB (added by zigzag, 13 years ago)
  • django/forms/fields.py

     
    584584
    585585    def to_python(self, value):
    586586        if value:
    587             url_fields = list(urlparse.urlsplit(value))
     587            try:
     588                value_splitted = urlparse.urlsplit(value)
     589            except ValueError:
     590                raise ValidationError(self.error_messages['invalid'])
     591
     592            url_fields = list(value_splitted)
    588593            if not url_fields[0]:
    589594                # If no URL scheme given, assume http://
    590595                url_fields[0] = 'http'
     
    596601                # Rebuild the url_fields list, since the domain segment may now
    597602                # contain the path too.
    598603                value = urlparse.urlunsplit(url_fields)
    599                 url_fields = list(urlparse.urlsplit(value))
     604                try:
     605                    value_splitted = urlparse.urlsplit(value)
     606                except ValueError:
     607                    raise ValidationError(self.error_messages['invalid'])
     608
     609                url_fields = list(value_splitted)
    600610            if not url_fields[2]:
    601611                # the path portion may need to be added before query params
    602612                url_fields[2] = '/'
  • tests/regressiontests/forms/tests/fields.py

     
    587587        self.assertEqual(u'http://valid-----hyphens.com/', f.clean('http://valid-----hyphens.com'))
    588588        self.assertEqual(u'http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah', f.clean('http://some.idn.xyzäöüßabc.domain.com:123/blah'))
    589589        self.assertEqual(u'http://www.example.com/s/http://code.djangoproject.com/ticket/13804', f.clean('www.example.com/s/http://code.djangoproject.com/ticket/13804'))
     590        self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, '[a')
     591        self.assertRaisesMessage(ValidationError, "[u'Enter a valid URL.']", f.clean, 'http://[a')
    590592
    591593    def test_url_regex_ticket11198(self):
    592594        f = URLField()
Back to Top