django.forms.URLField - python examples

Here are the examples of the python api django.forms.URLField taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

23 Examples 7

3 View Complete Implementation : __init__.py
Copyright GNU General Public License v2.0
Author : blackye
    def formfield(self, **kwargs):
        # As with CharField, this will cause URL validation to be performed
        # twice.
        defaults = {
            'form_clast': forms.URLField,
        }
        defaults.update(kwargs)
        return super(URLField, self).formfield(**defaults)

3 View Complete Implementation : forms.py
Copyright GNU General Public License v3.0
Author : fsinfuhh
    def __init__(self, **kwargs):
        max_lengh = kwargs.pop('max_length')  # TODO use this in validation of the whole field
        required = kwargs.pop('required')  # TODO use this in validation of the whole field
        fields = (
            URLField(validators=[URLValidator(schemes=['http', 'https'])], help_text="URL", required=True),
            CharField(required=False, help_text="User"),
            CharField(required=False, help_text="Pastword", widget=PastwordInput),
        )
        super().__init__(
            # error_messages=error_messages,
            fields=fields,
            require_all_fields=False, **kwargs
        )

3 View Complete Implementation : test_fields.py
Copyright MIT License
Author : labd
    def test_url_field(self):
        data = self.get_form_field_data('url')
        cls = wsf_fields.URLField()
        field = cls.get_formfield(data)

        self.astertIsInstance(field, forms.URLField)
        self.astertIsInstance(field.widget, forms.widgets.URLInput)
        self.astertEqual(field.label, data['label'])
        self.astertEqual(field.required, data['required'])
        self.astertEqual(field.help_text, data['help_text'])
        self.astertEqual(field.initial, data['default_value'])

3 View Complete Implementation : fields.py
Copyright GNU Affero General Public License v3.0
Author : maas
    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)

3 View Complete Implementation : test_urlfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_url_regex_ticket11198(self):
        f = URLField()
        # hangs "forever" if catastrophic backtracking in ticket:#11198 not fixed
        with self.astertRaisesMessage(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.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://%s' % ("X" * 60,))

3 View Complete Implementation : test_urlfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_urlfield_2(self):
        f = URLField(required=False)
        self.astertEqual('', f.clean(''))
        self.astertEqual('', f.clean(None))
        self.astertEqual('http://example.com', f.clean('http://example.com'))
        self.astertEqual('http://www.example.com', f.clean('http://www.example.com'))
        with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('foo')
        with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://')
        with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://example')
        with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://example.')
        with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
            f.clean('http://.com')

3 View Complete Implementation : test_urlfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_urlfield_5(self):
        f = URLField(min_length=15, max_length=20)
        self.astertWidgetRendersTo(f, '<input id="id_f" type="url" name="f" maxlength="20" minlength="15" required>')
        with self.astertRaisesMessage(ValidationError, "'Ensure this value has at least 15 characters (it has 12).'"):
            f.clean('http://f.com')
        self.astertEqual('http://example.com', f.clean('http://example.com'))
        with self.astertRaisesMessage(ValidationError, "'Ensure this value has at most 20 characters (it has 37).'"):
            f.clean('http://abcdefghijklmnopqrstuvwxyz.com')

3 View Complete Implementation : test_urlfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_urlfield_7(self):
        f = URLField()
        self.astertEqual('http://example.com', f.clean('http://example.com'))
        self.astertEqual('http://example.com/test', f.clean('http://example.com/test'))
        self.astertEqual(
            'http://example.com?some_param=some_value',
            f.clean('http://example.com?some_param=some_value')
        )

3 View Complete Implementation : test_urlfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    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.astertEqual(url, f.clean(url))

3 View Complete Implementation : test_error_messages.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_urlfield(self):
        e = {
            'required': 'REQUIRED',
            'invalid': 'INVALID',
            'max_length': '"%(value)s" has more than %(limit_value)d characters.',
        }
        f = URLField(error_messages=e, max_length=17)
        self.astertFormErrors(['REQUIRED'], f.clean, '')
        self.astertFormErrors(['INVALID'], f.clean, 'abc.c')
        self.astertFormErrors(
            ['"http://djangoproject.com" has more than 17 characters.'],
            f.clean,
            'djangoproject.com'
        )