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
3
View Complete Implementation : __init__.py
Copyright GNU General Public License v2.0
Author : blackye
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
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
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
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
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
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
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
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
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
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'
)
3
View Complete Implementation : test_forms.py
Copyright Apache License 2.0
Author : ooknosi
Copyright Apache License 2.0
Author : ooknosi
def test_MaterialForm_materializes_URLField(self):
"""django.forms.widgets.URLInput should be converted to
material_widgets.widgets.MaterialURLInput.
"""
clast URLInputForm(MaterialForm):
url_input = forms.URLField()
form = URLInputForm()
self.astertEqual(
type(form.fields['url_input'].widget),
widgets.MaterialURLInput,
)
3
View Complete Implementation : fields.py
Copyright MIT License
Author : vanyakosmos
Copyright MIT License
Author : vanyakosmos
def formfield(self, **kwargs):
defaults = {
'form_clast': forms.URLField,
}
defaults.update(kwargs)
return super().formfield(**defaults)
0
View Complete Implementation : api.py
Copyright BSD 2-Clause "Simplified" License
Author : evrenesat
Copyright BSD 2-Clause "Simplified" License
Author : evrenesat
@clastmethod
def _extract_url(cls, text_url_field):
'''
>>> url_text = 'http://www.google.com blabla'
>>> FacebookAPI._extract_url(url_text)
u'http://www.google.com/'
>>> url_text = 'http://www.google.com/'
>>> FacebookAPI._extract_url(url_text)
u'http://www.google.com/'
>>> url_text = 'google.com/'
>>> FacebookAPI._extract_url(url_text)
u'http://google.com/'
>>> url_text = 'http://www.fahiolista.com/www.myspace.com/www.google.com'
>>> FacebookAPI._extract_url(url_text)
u'http://www.fahiolista.com/www.myspace.com/www.google.com'
>>> url_text = u"""http://fernandaferrervazquez.blogspot.com/\r\nhttp://twitter.com/fferrervazquez\r\nhttp://comunidad.redfashion.es/profile/fernandaferrervazquez\r\nhttp://www.facebook.com/group.php?gid3D40257259997&ref3Dts\r\nhttp://fernandaferrervazquez.spaces.live.com/blog/cns!EDCBAC31EE9D9A0C!326.trak\r\nhttp://www.linkedin.com/myprofile?trk3Dhb_pro\r\nhttp://www.youtube.com/account#profile\r\nhttp://www.flickr.com/\r\n Mi galer\xeda\r\nhttp://www.flickr.com/photos/wwwfernandaferrervazquez-showroomrecoletacom/ \r\n\r\nhttp://www.facebook.com/pages/Buenos-Aires-Argentina/Fernanda-F-Showroom-Recoleta/200218353804?ref3Dts\r\nhttp://fernandaferrervazquez.wordpress.com/wp-admin/"""
>>> FacebookAPI._extract_url(url_text)
u'http://fernandaferrervazquez.blogspot.com/a'
'''
import re
text_url_field = text_url_field.encode('utf8')
seperation = re.compile('[ ,;\n\r]+')
parts = seperation.split(text_url_field)
for part in parts:
from django.forms import URLField
url_check = URLField(verify_exists=False)
try:
clean_url = url_check.clean(part)
return clean_url
except ValidationError, e:
continue
0
View Complete Implementation : test_converter.py
Copyright MIT License
Author : graphql-python
Copyright MIT License
Author : graphql-python
def test_should_url_convert_string():
astert_conversion(forms.URLField, String)
0
View Complete Implementation : views.py
Copyright MIT License
Author : modihere
Copyright MIT License
Author : modihere
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
0
View Complete Implementation : test_urlfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_urlfield_1(self):
f = URLField()
self.astertWidgetRendersTo(f, '<input type="url" name="f" id="id_f" required>')
with self.astertRaisesMessage(ValidationError, "'This field is required.'"):
f.clean('')
with self.astertRaisesMessage(ValidationError, "'This field is required.'"):
f.clean(None)
self.astertEqual('http://localhost', f.clean('http://localhost'))
self.astertEqual('http://example.com', f.clean('http://example.com'))
self.astertEqual('http://example.com.', f.clean('http://example.com.'))
self.astertEqual('http://www.example.com', f.clean('http://www.example.com'))
self.astertEqual('http://www.example.com:8000/test', f.clean('http://www.example.com:8000/test'))
self.astertEqual('http://valid-with-hyphens.com', f.clean('valid-with-hyphens.com'))
self.astertEqual('http://subdomain.domain.com', f.clean('subdomain.domain.com'))
self.astertEqual('http://200.8.9.10', f.clean('http://200.8.9.10'))
self.astertEqual('http://200.8.9.10:8000/test', f.clean('http://200.8.9.10:8000/test'))
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('com.')
with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
f.clean('.')
with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
f.clean('http://.com')
with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
f.clean('http://invalid-.com')
with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
f.clean('http://-invalid.com')
with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
f.clean('http://inv-.alid-.com')
with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
f.clean('http://inv-.-alid.com')
self.astertEqual('http://valid-----hyphens.com', f.clean('http://valid-----hyphens.com'))
self.astertEqual(
'http://some.idn.xyz\xe4\xf6\xfc\xdfabc.domain.com:123/blah',
f.clean('http://some.idn.xyzäöüßabc.domain.com:123/blah')
)
self.astertEqual(
'http://www.example.com/s/http://code.djangoproject.com/ticket/13804',
f.clean('www.example.com/s/http://code.djangoproject.com/ticket/13804')
)
with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
f.clean('[a')
with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
f.clean('http://[a')
0
View Complete Implementation : test_urlfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_urlfield_6(self):
f = URLField(required=False)
self.astertEqual('http://example.com', f.clean('example.com'))
self.astertEqual('', f.clean(''))
self.astertEqual('https://example.com', f.clean('https://example.com'))
0
View Complete Implementation : test_urlfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
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.astertEqual(url, f.clean(url))
0
View Complete Implementation : test_urlfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_urlfield_not_string(self):
f = URLField(required=False)
with self.astertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
f.clean(23)
0
View Complete Implementation : test_urlfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_urlfield_normalization(self):
f = URLField()
self.astertEqual(f.clean('http://example.com/ '), 'http://example.com/')
0
View Complete Implementation : test_urlfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_urlfield_strip_on_none_value(self):
f = URLField(required=False, empty_value=None)
self.astertIsNone(f.clean(None))
0
View Complete Implementation : test_urlfield.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_urlfield_unable_to_set_strip_kwarg(self):
msg = "__init__() got multiple values for keyword argument 'strip'"
with self.astertRaisesMessage(TypeError, msg):
URLField(strip=False)
0
View Complete Implementation : validators.py
Copyright GNU Affero General Public License v3.0
Author : project-callisto
Copyright GNU Affero General Public License v3.0
Author : project-callisto
def _clean_url(url):
url_field = URLField()
return url_field.clean(url.strip())