本文整理汇总了Python中django.forms.URLField.clean方法的典型用法代码示例。如果您正苦于以下问题:Python URLField.clean方法的具体用法?Python URLField.clean怎么用?Python URLField.clean使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms.URLField
的用法示例。
在下文中一共展示了URLField.clean方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_urlfield_7
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
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")
)
示例2: test_urlfield_5
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
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" />')
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')
示例3: test_urlfield_7
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
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')
)
示例4: test_url_regex_ticket11198
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
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,))
示例5: get_url_informations
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
def get_url_informations(url):
"""Get informations about a url"""
error_invalid = False, {"error": _(u"Invalid URL")}
error_exist = False, {"error": _(u"This URL has already been submitted")}
error_connect = False, {"error": _(u"Failed at opening the URL")}
# Check url is valid
try:
url_field = URLField()
clean_url = url_field.clean(url)
except:
return error_invalid
# Check url exist
if exist_url(clean_url):
return error_exist
# Get informations
req = requests.get(clean_url)
if 200 == req.status_code:
final_url = req.url
soup = BeautifulSoup(req.text)
title = soup.title.string
description = soup.findAll("meta", attrs={"name": re.compile("^description$", re.I)})[0].get("content")
else:
return error_connect
# Check final url exist if different from clean url
if final_url != clean_url:
if exist_url(final_url):
return error_exist
return True, {"url": final_url, "title": title, "description": description}
示例6: _extract_url
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
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
示例7: _extract_url
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
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'
'''
import re
text_url_field = str(text_url_field)
seperation = re.compile('[ |,|;]+')
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
示例8: test_urlfield_10
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
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:
self.assertEqual(url, f.clean(url))
示例9: test_urlfield_9
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
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:
# Valid IDN
self.assertEqual(url, f.clean(url))
示例10: get_url_informations
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
def get_url_informations(url):
"""Get informations about a url"""
error_invalid = False, {'error': _(u'Invalid URL')}
error_exist = False, {'error': _(u'This URL has already been submitted')}
error_connect = False, {'error': _(u'Failed at opening the URL')}
# Check url is valid
try:
url_field = URLField()
clean_url = url_field.clean(url)
except:
return error_invalid
# Check url exist
if exist_url(clean_url):
return error_exist
# Get informations
req = requests.get(clean_url)
if req.status_code == 200:
final_url = req.url
soup = BeautifulSoup(req.text)
title = soup.title.string
description = soup.findAll('meta',
attrs={'name': re.compile("^description$", re.I)})[0].get('content')
else:
return error_connect
# Check final url exist if different from clean url
if final_url != clean_url:
if exist_url(final_url):
return error_exist
return True, {
'url': final_url,
'title': title,
'description': description,
}
示例11: test_urlfield_normalization
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
def test_urlfield_normalization(self):
f = URLField()
self.assertEqual(f.clean('http://example.com/ '), 'http://example.com/')
示例12: test_urlfield_2
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
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")
示例13: test_urlfield_2
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
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')
示例14: test_urlfield_6
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
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"))
示例15: test_urlfield_not_string
# 需要导入模块: from django.forms import URLField [as 别名]
# 或者: from django.forms.URLField import clean [as 别名]
def test_urlfield_not_string(self):
f = URLField(required=False)
with self.assertRaisesMessage(ValidationError, "'Enter a valid URL.'"):
f.clean(23)