diff --git a/django/newforms/fields.py b/django/newforms/fields.py index a2d8afbdeb..522a4c9bf7 100644 --- a/django/newforms/fields.py +++ b/django/newforms/fields.py @@ -416,6 +416,9 @@ class URLField(RegexField): self.user_agent = validator_user_agent def clean(self, value): + # If no URL scheme given, assume http:// + if value and '://' not in value: + value = u'http://%s' % value value = super(URLField, self).clean(value) if value == u'': return value diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 2cbe5a4400..fef8361a14 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -1623,10 +1623,6 @@ u'http://200.8.9.10:8000/test' Traceback (most recent call last): ... ValidationError: [u'Enter a valid URL.'] ->>> f.clean('example.com') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] >>> f.clean('http://') Traceback (most recent call last): ... @@ -1657,10 +1653,6 @@ u'http://www.example.com' Traceback (most recent call last): ... ValidationError: [u'Enter a valid URL.'] ->>> f.clean('example.com') -Traceback (most recent call last): -... -ValidationError: [u'Enter a valid URL.'] >>> f.clean('http://') Traceback (most recent call last): ... @@ -1714,6 +1706,15 @@ Traceback (most recent call last): ... ValidationError: [u'Ensure this value has at most 20 characters (it has 37).'] +URLField should prepend 'http://' if no scheme was given +>>> f = URLField(required=False) +>>> f.clean('example.com') +u'http://example.com' +>>> f.clean('') +u'' +>>> f.clean('https://example.com') +u'https://example.com' + # BooleanField ################################################################ >>> f = BooleanField()