Django

Code

Ticket #5331: urlfield.2.patch

File urlfield.2.patch, 1.7 kB (added by SmileyChris, 1 year ago)

with tests

  • django/newforms/fields.py

    old new  
    405405        self.user_agent = validator_user_agent 
    406406 
    407407    def clean(self, value): 
     408        # If no URL scheme given, assume http:// 
     409        if value and ':' not in value: 
     410            value = u'http://%s' % value 
    408411        value = super(URLField, self).clean(value) 
    409412        if value == u'': 
    410413            return value 
  • tests/regressiontests/forms/tests.py

    old new  
    15891589Traceback (most recent call last): 
    15901590... 
    15911591ValidationError: [u'Enter a valid URL.'] 
    1592 >>> f.clean('example.com') 
    1593 Traceback (most recent call last): 
    1594 ... 
    1595 ValidationError: [u'Enter a valid URL.'] 
    15961592>>> f.clean('http://') 
    15971593Traceback (most recent call last): 
    15981594... 
     
    16231619Traceback (most recent call last): 
    16241620... 
    16251621ValidationError: [u'Enter a valid URL.'] 
    1626 >>> f.clean('example.com') 
    1627 Traceback (most recent call last): 
    1628 ... 
    1629 ValidationError: [u'Enter a valid URL.'] 
    16301622>>> f.clean('http://') 
    16311623Traceback (most recent call last): 
    16321624... 
     
    16801672... 
    16811673ValidationError: [u'Ensure this value has at most 20 characters (it has 37).'] 
    16821674 
     1675URLField should prepend 'http://' if no scheme was given 
     1676>>> f = URLField(required=False) 
     1677>>> f.clean('example.com') 
     1678u'http://example.com' 
     1679>>> f.clean('') 
     1680u'' 
     1681>>> f.clean('https://example.com') 
     1682u'https://example.com' 
     1683 
    16831684# BooleanField ################################################################ 
    16841685 
    16851686>>> f = BooleanField()