- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要一组字符串的精确定义,这些字符串被视为 models.URLField 的合法 URL。 .
就我而言,精确的定义可以是正则表达式,也可以是验证 URL 有效性的一段代码,或者任何其他形式的正式准确解释。
最佳答案
查看源代码,似乎 URLField
是一个 CharField
,它将 validators.URLValidator
添加到字段的验证器列表中,并且将其导入来自django.core
。
来自site-packages/django/core/validators.py
:
class URLValidator(RegexValidator):
regex = re.compile(
r'^https?://' # http:// or https://
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+[A-Z]{2,6}\.?|' #domain...
r'localhost|' #localhost...
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
r'(?::\d+)?' # optional port
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
def __init__(self, verify_exists=False, validator_user_agent=URL_VALIDATOR_USER_AGENT):
super(URLValidator, self).__init__()
self.verify_exists = verify_exists
self.user_agent = validator_user_agent
def __call__(self, value):
try:
super(URLValidator, self).__call__(value)
except ValidationError, e:
# Trivial case failed. Try for possible IDN domain
if value:
value = smart_unicode(value)
scheme, netloc, path, query, fragment = urlparse.urlsplit(value)
try:
netloc = netloc.encode('idna') # IDN -> ACE
except UnicodeError: # invalid domain part
raise e
url = urlparse.urlunsplit((scheme, netloc, path, query, fragment))
super(URLValidator, self).__call__(url)
else:
raise
else:
url = value
if self.verify_exists:
import urllib2
headers = {
"Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
"Accept-Language": "en-us,en;q=0.5",
"Accept-Charset": "ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"Connection": "close",
"User-Agent": self.user_agent,
}
try:
req = urllib2.Request(url, None, headers)
u = urllib2.urlopen(req)
except ValueError:
raise ValidationError(_(u'Enter a valid URL.'), code='invalid')
except: # urllib2.URLError, httplib.InvalidURL, etc.
raise ValidationError(_(u'This URL appears to be a broken link.'), code='invalid_link')
因此它会尝试验证正则表达式,如果失败,它会使用 Python 的 urlparse
模块取消 IDN 域名并重试。
关于python - 在 Django 中填充 URLFields 的合法值的确切模式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5771472/
(注意:这个问题的动机是试图提出预处理器黑客来生成无操作分配来回答另一个问题: Macro that accept new object ...请记住这一点!) 这是一个人为的类: class foo
所以我尝试用java编写一个井字棋游戏。大部分已经完成,但是,如果有人选择了已被占用的空间,我无法返回无效的移动。 这是我试图弄清楚的代码。我认为由于空间是由数字 0 表示的(我的教授告诉我们这一点)
我想使用 Java 代码获取某些 Google 搜索引擎查询(在整个网络上)的估计结果数。 我每天只需要做很少的查询,所以一开始 Google Web Search API ,虽然已被弃用,但似乎已经
C++ Primer, 5e 的练习 4.20 询问表达式 iter++->empty(); 是否成立是合法的。假设iter是 vector::iterator . 这个表达式是合法的。我用gcc编译
C++ Primer, 5e 的练习 4.20 询问表达式 iter++->empty(); 是否成立是合法的。假设iter是 vector::iterator . 这个表达式是合法的。我用gcc编译
在 C++ 中,动态内存(取消)分配(malloc-free/new-delete)显然可以重复获得相同的内存范围,该内存范围被释放并按顺序再次分配。在多线程 C++ 中,这可能发生在多个线程中。 显
AesCryptoServiceProvider.LegalKeySizes 字段显示允许的位大小。 但是我不明白的是,如果这些都是真的,我如何才能成功利用 2048 位 key 长度(256 字节)
我有一个结构: typedef struct _n { int type; union { char *s; int i; }; } n; 当我
这个问题在这里已经有了答案: 关闭10年前. Possible Duplicate: int var = 1; void main() { int i = i; } 以下代码在g++和Visual C
根据 C++03 标准 (5.3.4/7): When the value of the expression in a direct-new-declarator is zero, the allo
我正在尝试创建一个生成器,使用 scalacheck 1.6.6 和规范 1.7 (scala 2.8.1) 生成(非零长度)合法的 unicode 字符串。 我希望我可以创建像这样的生成器: obj
这确实是一个关于 C++ 的问题,而不是 OpenGL。我正在关注 this OpenGL 教程(我刚开始),作者使用 C++(不是 C)。我的问题是 glVertexAttribPointer ta
由于 C++ 别名规则,您不能随意将 (一个 int*) 指向 char 数组,这似乎达成了一些共识。 从另一个问题——Generic char[] based storage and avoidin
我正在寻找一个通过 HTTP 接口(interface)发送文本消息的 SMS 网关。 但是,有些短信网关不允许设置发件人ID,允许的要么要求用户证明发件人ID是自己的手机号码,要么手动检查每个发件人
我们注意到您的应用要求用户注册个人信息才能访问非基于帐户的功能。在允许访问与用户无关的应用内容和功能之前,应用不能要求用户注册。 我们在注册时采用电子邮件 ID,因为所有数据都代表电子邮件 ID 在后
我一直在阅读 Bloch 和 Gafter 的 Java Puzzlers,并读到了第 10 个谜题 (Tweedledee)。这个谜题的本质是 provide declarations for th
这个问题确实符合标题:我很想知道造成这种差异的技术原因是什么,以及基本原理? std::shared_ptr sharedToVoid; // legal; std::unique_ptr uniqu
我试图做的是将数据框数据附加到现有的合法 excel 文件中。我使用了 openpyxl 中的 load_workbook() 函数,但它系统地返回了一个错误。这是一些在我的机器上崩溃的代码: fro
App 多次被 Apple 拒绝 “经过进一步审查,我们仍然发现您的应用使用了后台定位服务,但并未按照 iOS 人机界面指南的要求在位置模式警报中阐明其使用目的。” 在“NSLocationAlway
据我所知,像这样在 C++ 中实例化一个整数是合法的: int x = int(5); 作为一名 Java 程序员,我会假设这行代码调用传递“5”作为参数的整数的构造函数。我读到 int 不是一个类,
我是一名优秀的程序员,十分优秀!