From 20ecbbd9e749f7abd0a7ffcc61b01cb874846817 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Fri, 14 Sep 2007 07:02:55 +0000 Subject: [PATCH] Fiex #5331 -- Modified newforms URLField to prepend http:// if no protocol is specified by the user. Thanks, SmileyChris. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6173 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/fields.py | 3 +++ tests/regressiontests/forms/tests.py | 17 +++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) 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()