Python django.forms.URLField() Examples

The following are 30 code examples for showing how to use django.forms.URLField(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.forms , or try the search function .

Example 1
Project: djongo   Author: nesdis   File: test_urlfield.py    License: 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
Project: djongo   Author: nesdis   File: test_urlfield.py    License: 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
Project: djongo   Author: nesdis   File: test_urlfield.py    License: 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 4
Project: djongo   Author: nesdis   File: test_urlfield.py    License: 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 5
Project: coursys   Author: sfu-fas   File: other.py    License: 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 6
Project: GTDWeb   Author: lanbing510   File: __init__.py    License: 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
Project: GTDWeb   Author: lanbing510   File: __init__.py    License: 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 8
Project: GTDWeb   Author: lanbing510   File: __init__.py    License: 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 9
Project: bioforum   Author: reBiocoder   File: __init__.py    License: 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 10
Project: Hands-On-Application-Development-with-PyCharm   Author: PacktPublishing   File: __init__.py    License: 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,
        }) 
Example 11
Project: wagtail   Author: wagtail   File: field_block.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, required=True, help_text=None, max_length=None, min_length=None, validators=(), **kwargs):
        self.field = forms.URLField(
            required=required,
            help_text=help_text,
            max_length=max_length,
            min_length=min_length,
            validators=validators,
        )
        super().__init__(**kwargs) 
Example 12
Project: wagtailstreamforms   Author: labd   File: test_fields.py    License: MIT License 5 votes vote down vote up
def test_url_field(self):
        data = self.get_form_field_data("url")
        cls = wsf_fields.URLField()
        field = cls.get_formfield(data)

        self.assertIsInstance(field, forms.URLField)
        self.assertIsInstance(field.widget, forms.widgets.URLInput)
        self.assertEqual(field.label, data["label"])
        self.assertEqual(field.required, data["required"])
        self.assertEqual(field.help_text, data["help_text"])
        self.assertEqual(field.initial, data["default_value"]) 
Example 13
Project: python   Author: Yeah-Kun   File: __init__.py    License: 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 14
Project: python   Author: Yeah-Kun   File: __init__.py    License: Apache License 2.0 5 votes vote down vote up
def deconstruct(self):
        name, path, args, kwargs = super(URLField, self).deconstruct()
        if kwargs.get("max_length") == 200:
            del kwargs['max_length']
        return name, path, args, kwargs 
Example 15
Project: python   Author: Yeah-Kun   File: __init__.py    License: Apache License 2.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 16
Project: luscan-devel   Author: blackye   File: __init__.py    License: 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 17
Project: openhgsenti   Author: drexly   File: __init__.py    License: 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 18
Project: openhgsenti   Author: drexly   File: __init__.py    License: Apache License 2.0 5 votes vote down vote up
def deconstruct(self):
        name, path, args, kwargs = super(URLField, self).deconstruct()
        if kwargs.get("max_length") == 200:
            del kwargs['max_length']
        return name, path, args, kwargs 
Example 19
Project: openhgsenti   Author: drexly   File: __init__.py    License: Apache License 2.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 20
Project: python2017   Author: bpgc-cte   File: __init__.py    License: MIT License 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 21
Project: python2017   Author: bpgc-cte   File: __init__.py    License: MIT License 5 votes vote down vote up
def deconstruct(self):
        name, path, args, kwargs = super(URLField, self).deconstruct()
        if kwargs.get("max_length") == 200:
            del kwargs['max_length']
        return name, path, args, kwargs 
Example 22
Project: python2017   Author: bpgc-cte   File: __init__.py    License: 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(URLField, self).formfield(**defaults) 
Example 23
Project: graphene-django   Author: graphql-python   File: test_converter.py    License: MIT License 5 votes vote down vote up
def test_should_url_convert_string():
    assert_conversion(forms.URLField, String) 
Example 24
Project: callisto-core   Author: project-callisto   File: validators.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def _clean_url(url):
    url_field = URLField()
    return url_field.clean(url.strip()) 
Example 25
Project: python-compat-runtime   Author: GoogleCloudPlatform   File: djangoforms.py    License: Apache License 2.0 5 votes vote down vote up
def get_form_field(self, **kwargs):
    """Return a Django form field appropriate for a URL property.

    This defaults to a URLField instance.
    """
    defaults = {'form_class': forms.URLField}
    defaults.update(kwargs)
    return super(LinkProperty, self).get_form_field(**defaults) 
Example 26
Project: logtacts   Author: phildini   File: forms.py    License: MIT License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        document_items = None
        self.user = kwargs.pop('user', None)
        if kwargs.get('data'):
            document_items = dict((key, value) for key, value in kwargs.get('data').items() if key.startswith('document'))
        self.book = kwargs.pop('book')
        tag_choices = kwargs.pop('tag_choices')
        super(ContactForm, self).__init__(*args, **kwargs)
        choices = Tag.objects.filter(book=self.book).values_list('id', 'tag')
        self.fields['tags'].choices = tag_choices
        self.fields['deleted_fields'] = forms.CharField(required=False)
        if document_items:
            for item in document_items:
                parts = item.split('_')
                field_category = parts[1]
                if len(parts) == 4:
                    if parts[3] == 'pref':
                        self.fields[item] = forms.BooleanField()
                    if parts[3] == 'label':
                        self.fields[item] = forms.CharField(max_length=255)
                else:
                    if field_category == contact_constants.FIELD_TYPE_EMAIL:
                        self.fields[item] = forms.EmailField(max_length=255)
                    if field_category == contact_constants.FIELD_TYPE_URL:
                        self.fields[item] = forms.URLField(max_length=255)
                    if field_category == contact_constants.FIELD_TYPE_DATE:
                        self.fields[item] = forms.DateField()
                    else:
                        self.fields[item] = forms.CharField() 
Example 27
Project: hypha   Author: OpenTechFund   File: blocks.py    License: BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def get_field_class(self, struct_value):
        text_format = struct_value['format']
        if text_format == 'url':
            return forms.URLField
        if text_format == 'email':
            return forms.EmailField
        return super().get_field_class(struct_value) 
Example 28
Project: djongo   Author: nesdis   File: tests.py    License: 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 29
Project: djongo   Author: nesdis   File: test_urlfield.py    License: 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 30
Project: djongo   Author: nesdis   File: test_urlfield.py    License: 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')