Django

Code

Changeset 8089

Show
Ignore:
Timestamp:
07/26/08 00:18:39 (4 months ago)
Author:
mtredinnick
Message:

Fixed #7345 -- When normalising the URLField form field, attach a trailing
slash when only a host (no path) is given. Thanks, jpwatts.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/forms/fields.py

    r8046 r8089  
    88import re 
    99import time 
     10import urlparse 
    1011try: 
    1112    from cStringIO import StringIO 
     
    540541        if value and '://' not in value: 
    541542            value = u'http://%s' % value 
     543        # If no URL path given, assume / 
     544        if value and not urlparse.urlsplit(value).path: 
     545            value += '/' 
    542546        value = super(URLField, self).clean(value) 
    543547        if value == u'': 
  • django/trunk/tests/regressiontests/forms/fields.py

    r7987 r8089  
    821821ValidationError: [u'This field is required.'] 
    822822>>> f.clean('http://localhost') 
    823 u'http://localhost
     823u'http://localhost/
    824824>>> f.clean('http://example.com') 
    825 u'http://example.com
     825u'http://example.com/
    826826>>> f.clean('http://www.example.com') 
    827 u'http://www.example.com
     827u'http://www.example.com/
    828828>>> f.clean('http://www.example.com:8000/test') 
    829829u'http://www.example.com:8000/test' 
    830830>>> f.clean('http://200.8.9.10') 
    831 u'http://200.8.9.10
     831u'http://200.8.9.10/
    832832>>> f.clean('http://200.8.9.10:8000/test') 
    833833u'http://200.8.9.10:8000/test' 
     
    859859u'' 
    860860>>> f.clean('http://example.com') 
    861 u'http://example.com
     861u'http://example.com/
    862862>>> f.clean('http://www.example.com') 
    863 u'http://www.example.com
     863u'http://www.example.com/
    864864>>> f.clean('foo') 
    865865Traceback (most recent call last): 
     
    887887>>> f = URLField(verify_exists=True) 
    888888>>> f.clean('http://www.google.com') # This will fail if there's no Internet connection 
    889 u'http://www.google.com
     889u'http://www.google.com/
    890890>>> f.clean('http://example') 
    891891Traceback (most recent call last): 
     
    904904u'' 
    905905>>> f.clean('http://www.google.com') # This will fail if there's no Internet connection 
    906 u'http://www.google.com
     906u'http://www.google.com/
    907907 
    908908URLField also access min_length and max_length parameters, for convenience. 
     
    911911Traceback (most recent call last): 
    912912... 
    913 ValidationError: [u'Ensure this value has at least 15 characters (it has 12).'] 
     913ValidationError: [u'Ensure this value has at least 15 characters (it has 13).'] 
    914914>>> f.clean('http://example.com') 
    915 u'http://example.com
     915u'http://example.com/
    916916>>> f.clean('http://abcdefghijklmnopqrstuvwxyz.com') 
    917917Traceback (most recent call last): 
    918918... 
    919 ValidationError: [u'Ensure this value has at most 20 characters (it has 37).'] 
     919ValidationError: [u'Ensure this value has at most 20 characters (it has 38).'] 
    920920 
    921921URLField should prepend 'http://' if no scheme was given 
    922922>>> f = URLField(required=False) 
    923923>>> f.clean('example.com') 
    924 u'http://example.com
     924u'http://example.com/
    925925>>> f.clean('') 
    926926u'' 
    927927>>> f.clean('https://example.com') 
    928 u'https://example.com' 
     928u'https://example.com/' 
     929 
     930URLField should append '/' if no path was given 
     931>>> f = URLField() 
     932>>> f.clean('http://example.com') 
     933u'http://example.com/' 
     934 
     935URLField shouldn't change the path if it was given 
     936>>> f.clean('http://example.com/test') 
     937u'http://example.com/test' 
    929938 
    930939# BooleanField ################################################################