Python django.forms.URLField() Examples

The following are 30 code examples of django.forms.URLField(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.forms , or try the search function .
Example #1
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_urlfield_2(self):
        f = URLField(required=False)
        self.assertEqual('', f.clean(''))
        self.assertEqual('', f.clean(None))
        self.assertEqual('http://example.com', f.clean('http://example.com'))
        self.assertEqual('http://www.example.com', f.clean('http://www.example.com'))
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('foo')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://example')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://example.')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://.com') 
Example #2
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_urlfield_9(self):
        f = URLField()
        urls = (
            'http://עברית.idn.icann.org/',
            'http://sãopaulo.com/',
            'http://sãopaulo.com.br/',
            'http://пример.испытание/',
            'http://مثال.إختبار/',
            'http://例子.测试/',
            'http://例子.測試/',
            'http://उदाहरण.परीक्षा/',
            'http://例え.テスト/',
            'http://مثال.آزمایشی/',
            'http://실례.테스트/',
            'http://العربية.idn.icann.org/',
        )
        for url in urls:
            with self.subTest(url=url):
                # Valid IDN
                self.assertEqual(url, f.clean(url)) 
Example #3
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_urlfield_9(self):
        f = URLField()
        urls = (
            'http://עברית.idn.icann.org/',
            'http://sãopaulo.com/',
            'http://sãopaulo.com.br/',
            'http://пример.испытание/',
            'http://مثال.إختبار/',
            'http://例子.测试/',
            'http://例子.測試/',
            'http://उदाहरण.परीक्षा/',
            'http://例え.テスト/',
            'http://مثال.آزمایشی/',
            'http://실례.테스트/',
            'http://العربية.idn.icann.org/',
        )
        for url in urls:
            with self.subTest(url=url):
                # Valid IDN
                self.assertEqual(url, f.clean(url)) 
Example #4
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_urlfield_2(self):
        f = URLField(required=False)
        self.assertEqual('', f.clean(''))
        self.assertEqual('', f.clean(None))
        self.assertEqual('http://example.com', f.clean('http://example.com'))
        self.assertEqual('http://www.example.com', f.clean('http://www.example.com'))
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('foo')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://example')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://example.')
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://.com') 
Example #5
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_url_regex_ticket11198(self):
        f = URLField()
        # hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://%s' % ("X" * 200,))

        # a second test, to make sure the problem is really addressed, even on
        # domains that don't fail the domain label length check in the regex
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://%s' % ("X" * 60,)) 
Example #6
Source File: __init__.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, verbose_name=None, name=None, **kwargs):
        kwargs['max_length'] = kwargs.get('max_length', 200)
        super(URLField, self).__init__(verbose_name, name, **kwargs) 
Example #7
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_urlfield_5(self):
        f = URLField(min_length=15, max_length=20)
        self.assertWidgetRendersTo(f, '<input id="id_f" type="url" name="f" maxlength="20" minlength="15" required>')
        with self.assertRaisesMessage(ValidationError, "'Ensure this value has at least 15 characters (it has 12).'"):
            f.clean('http://f.com')
        self.assertEqual('http://example.com', f.clean('http://example.com'))
        with self.assertRaisesMessage(ValidationError, "'Ensure this value has at most 20 characters (it has 37).'"):
            f.clean('http://abcdefghijklmnopqrstuvwxyz.com') 
Example #8
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_urlfield_7(self):
        f = URLField()
        self.assertEqual('http://example.com', f.clean('http://example.com'))
        self.assertEqual('http://example.com/test', f.clean('http://example.com/test'))
        self.assertEqual(
            'http://example.com?some_param=some_value',
            f.clean('http://example.com?some_param=some_value')
        ) 
Example #9
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_urlfield_10(self):
        """URLField correctly validates IPv6 (#18779)."""
        f = URLField()
        urls = (
            'http://[12:34::3a53]/',
            'http://[a34:9238::]:8080/',
        )
        for url in urls:
            with self.subTest(url=url):
                self.assertEqual(url, f.clean(url)) 
Example #10
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_urlfield_not_string(self):
        f = URLField(required=False)
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean(23) 
Example #11
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_urlfield_normalization(self):
        f = URLField()
        self.assertEqual(f.clean('http://example.com/     '), 'http://example.com/') 
Example #12
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_urlfield_unable_to_set_strip_kwarg(self):
        msg = "__init__() got multiple values for keyword argument 'strip'"
        with self.assertRaisesMessage(TypeError, msg):
            URLField(strip=False) 
Example #13
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_field_type_overrides(self):
        form = FieldOverridesByFormMetaForm()
        self.assertIs(Category._meta.get_field('url').__class__, models.CharField)
        self.assertIsInstance(form.fields['url'], forms.URLField) 
Example #14
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_url_regex_ticket11198(self):
        f = URLField()
        # hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://%s' % ("X" * 200,))

        # a second test, to make sure the problem is really addressed, even on
        # domains that don't fail the domain label length check in the regex
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://%s' % ("X" * 60,)) 
Example #15
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_urlfield_6(self):
        f = URLField(required=False)
        self.assertEqual('http://example.com', f.clean('example.com'))
        self.assertEqual('', f.clean(''))
        self.assertEqual('https://example.com', f.clean('https://example.com')) 
Example #16
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_urlfield_7(self):
        f = URLField()
        self.assertEqual('http://example.com', f.clean('http://example.com'))
        self.assertEqual('http://example.com/test', f.clean('http://example.com/test'))
        self.assertEqual(
            'http://example.com?some_param=some_value',
            f.clean('http://example.com?some_param=some_value')
        ) 
Example #17
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_urlfield_10(self):
        """URLField correctly validates IPv6 (#18779)."""
        f = URLField()
        urls = (
            'http://[12:34::3a53]/',
            'http://[a34:9238::]:8080/',
        )
        for url in urls:
            with self.subTest(url=url):
                self.assertEqual(url, f.clean(url)) 
Example #18
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_urlfield_not_string(self):
        f = URLField(required=False)
        with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean(23) 
Example #19
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_urlfield_strip_on_none_value(self):
        f = URLField(required=False, empty_value=None)
        self.assertIsNone(f.clean(None)) 
Example #20
Source File: test_urlfield.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_urlfield_unable_to_set_strip_kwarg(self):
        msg = "__init__() got multiple values for keyword argument 'strip'"
        with self.assertRaisesMessage(TypeError, msg):
            URLField(strip=False) 
Example #21
Source File: fields.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def to_python(self, value):
        # Call grandparent method (CharField) to get string value.
        value = super(forms.URLField, self).to_python(value)
        # If it's a PPA locator, return it, else run URL pythonator.
        match = re.search(URLOrPPAValidator.ppa_re, value)
        return value if match else super().to_python(value) 
Example #22
Source File: fields.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def formfield(self, **kwargs):
        defaults = {"form_class": URLOrPPAFormField}
        defaults.update(kwargs)
        return super(URLField, self).formfield(**defaults) 
Example #23
Source File: forms.py    From djangocms-forms with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def prepare_url(self, field):
        field_attrs = field.build_field_attrs()
        widget_attrs = field.build_widget_attrs()

        field_attrs.update({
            'widget': forms.URLInput(attrs=widget_attrs)
        })
        return forms.URLField(**field_attrs) 
Example #24
Source File: views.py    From shortweb with MIT License 5 votes vote down vote up
def validate_url(url):
	protocols=[
		'http://',
		'https://',
	]
	flag=0
	url_form_field = URLField()
	email_field = EmailField()
	try :
		email_field.clean(url)
	except ValidationError:
		if url!='':
			for protocol in protocols:
				n=len(protocol)
				if url[0:n]==protocol:
					flag=1
					break
			if flag==0:
				flag1=1
				for protocol in protocols:
					new_url = protocol+url
					try:
						new_url == url_form_field.clean(new_url)
					except ValidationError:
						flag1=0
					else:
						url=new_url
						break
				if flag1==1:
					return True,url
				return False,url
			return True,url
		return False,url
	else:
		return False,url 
Example #25
Source File: __init__.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def __init__(self, verbose_name=None, name=None, **kwargs):
        kwargs['max_length'] = kwargs.get('max_length', 200)
        super(URLField, self).__init__(verbose_name, name, **kwargs) 
Example #26
Source File: other.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def make_entry_field(self, fieldsubmission=None):
        c = forms.URLField(required=self.config['required'],
            label=self.config['label'],
            help_text=self.config['help_text'])

        if fieldsubmission:
            c.initial = fieldsubmission.data['info']

        return c 
Example #27
Source File: __init__.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def deconstruct(self):
        name, path, args, kwargs = super(URLField, self).deconstruct()
        if kwargs.get("max_length", None) == 200:
            del kwargs['max_length']
        return name, path, args, kwargs 
Example #28
Source File: __init__.py    From GTDWeb with GNU General Public License v2.0 5 votes vote down vote up
def formfield(self, **kwargs):
        # As with CharField, this will cause URL validation to be performed
        # twice.
        defaults = {
            'form_class': forms.URLField,
        }
        defaults.update(kwargs)
        return super(URLField, self).formfield(**defaults) 
Example #29
Source File: __init__.py    From bioforum with MIT License 5 votes vote down vote up
def formfield(self, **kwargs):
        # As with CharField, this will cause URL validation to be performed
        # twice.
        defaults = {
            'form_class': forms.URLField,
        }
        defaults.update(kwargs)
        return super().formfield(**defaults) 
Example #30
Source File: __init__.py    From Hands-On-Application-Development-with-PyCharm with MIT License 5 votes vote down vote up
def formfield(self, **kwargs):
        # As with CharField, this will cause URL validation to be performed
        # twice.
        return super().formfield(**{
            'form_class': forms.URLField,
            **kwargs,
        })