diff --git a/django/newforms/fields.py b/django/newforms/fields.py
index 08e8b84..57f2341 100644
a
|
b
|
import datetime
|
7 | 7 | import os |
8 | 8 | import re |
9 | 9 | import time |
| 10 | import urlparse |
10 | 11 | # Python 2.3 fallbacks |
11 | 12 | try: |
12 | 13 | from decimal import Decimal, DecimalException |
… |
… |
class URLField(RegexField):
|
509 | 510 | # If no URL scheme given, assume http:// |
510 | 511 | if value and '://' not in value: |
511 | 512 | value = u'http://%s' % value |
| 513 | # If no URL path given, assume / |
| 514 | if value and not urlparse.urlsplit(value).path: |
| 515 | value += '/' |
512 | 516 | value = super(URLField, self).clean(value) |
513 | 517 | if value == u'': |
514 | 518 | return value |
diff --git a/tests/regressiontests/forms/fields.py b/tests/regressiontests/forms/fields.py
index f3b6a96..c4e732e 100644
a
|
b
|
Traceback (most recent call last):
|
816 | 816 | ... |
817 | 817 | ValidationError: [u'This field is required.'] |
818 | 818 | >>> f.clean('http://localhost') |
819 | | u'http://localhost' |
| 819 | u'http://localhost/' |
820 | 820 | >>> f.clean('http://example.com') |
821 | | u'http://example.com' |
| 821 | u'http://example.com/' |
822 | 822 | >>> f.clean('http://www.example.com') |
823 | | u'http://www.example.com' |
| 823 | u'http://www.example.com/' |
824 | 824 | >>> f.clean('http://www.example.com:8000/test') |
825 | 825 | u'http://www.example.com:8000/test' |
826 | 826 | >>> f.clean('http://200.8.9.10') |
827 | | u'http://200.8.9.10' |
| 827 | u'http://200.8.9.10/' |
828 | 828 | >>> f.clean('http://200.8.9.10:8000/test') |
829 | 829 | u'http://200.8.9.10:8000/test' |
830 | 830 | >>> f.clean('foo') |
… |
… |
u''
|
854 | 854 | >>> f.clean(None) |
855 | 855 | u'' |
856 | 856 | >>> f.clean('http://example.com') |
857 | | u'http://example.com' |
| 857 | u'http://example.com/' |
858 | 858 | >>> f.clean('http://www.example.com') |
859 | | u'http://www.example.com' |
| 859 | u'http://www.example.com/' |
860 | 860 | >>> f.clean('foo') |
861 | 861 | Traceback (most recent call last): |
862 | 862 | ... |
… |
… |
URLField takes an optional verify_exists parameter, which is False by default.
|
882 | 882 | This verifies that the URL is live on the Internet and doesn't return a 404 or 500: |
883 | 883 | >>> f = URLField(verify_exists=True) |
884 | 884 | >>> f.clean('http://www.google.com') # This will fail if there's no Internet connection |
885 | | u'http://www.google.com' |
| 885 | u'http://www.google.com/' |
886 | 886 | >>> f.clean('http://example') |
887 | 887 | Traceback (most recent call last): |
888 | 888 | ... |
… |
… |
ValidationError: [u'This URL appears to be a broken link.']
|
899 | 899 | >>> f.clean('') |
900 | 900 | u'' |
901 | 901 | >>> f.clean('http://www.google.com') # This will fail if there's no Internet connection |
902 | | u'http://www.google.com' |
| 902 | u'http://www.google.com/' |
903 | 903 | |
904 | 904 | URLField also access min_length and max_length parameters, for convenience. |
905 | 905 | >>> f = URLField(min_length=15, max_length=20) |
906 | 906 | >>> f.clean('http://f.com') |
907 | 907 | Traceback (most recent call last): |
908 | 908 | ... |
909 | | ValidationError: [u'Ensure this value has at least 15 characters (it has 12).'] |
| 909 | ValidationError: [u'Ensure this value has at least 15 characters (it has 13).'] |
910 | 910 | >>> f.clean('http://example.com') |
911 | | u'http://example.com' |
| 911 | u'http://example.com/' |
912 | 912 | >>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com') |
913 | 913 | Traceback (most recent call last): |
914 | 914 | ... |
915 | | ValidationError: [u'Ensure this value has at most 20 characters (it has 37).'] |
| 915 | ValidationError: [u'Ensure this value has at most 20 characters (it has 38).'] |
916 | 916 | |
917 | 917 | URLField should prepend 'http://' if no scheme was given |
918 | 918 | >>> f = URLField(required=False) |
919 | 919 | >>> f.clean('example.com') |
920 | | u'http://example.com' |
| 920 | u'http://example.com/' |
921 | 921 | >>> f.clean('') |
922 | 922 | u'' |
923 | 923 | >>> f.clean('https://example.com') |
924 | | u'https://example.com' |
| 924 | u'https://example.com/' |
| 925 | |
| 926 | URLField should append '/' if no path was given |
| 927 | >>> f = URLField() |
| 928 | >>> f.clean('http://example.com') |
| 929 | u'http://example.com/' |
| 930 | |
| 931 | URLField shouldn't change the path if it was given |
| 932 | >>> f.clean('http://example.com/test') |
| 933 | u'http://example.com/test' |
925 | 934 | |
926 | 935 | # BooleanField ################################################################ |
927 | 936 | |