您是否在尝试验证该网站的同一台服务器上托管该网站?docs http://docs.djangoproject.com/en/dev/ref/models/fields/#urlfieldim
请注意,当您使用
单线程开发服务器,
验证正在提供服务的 URL
同一服务器将挂起。这不应该
对于多线程来说是一个问题
服务器。
它看起来不像表单级别的验证失败
>>> from django import forms
>>> f = forms.URLField()
>>> f.clean('http://www.nacolmeia.com.br/do/Home/oferta/EnER')
u'http://www.nacolmeia.com.br/do/Home/oferta/EnER'
>>> f.clean('sadfas')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/dev/.virtualenvs/thepidb/lib/python2.7/site-packages/django/forms/fields.py", line 171, in clean
self.run_validators(value)
File "/home/dev/.virtualenvs/thepidb/lib/python2.7/site-packages/django/forms/fields.py", line 160, in run_validators
raise ValidationError(errors)
ValidationError: [u'Enter a valid URL.']
>>>
如果您不需要验证网站不会返回 404,请在您的 models.py 中
url = models.URLField(verify_exists=False)
edit:
在深入研究 django 源代码之后here http://code.djangoproject.com/browser/django/trunk/django/core/validators.py?rev=15504以及一些与 shell 的混淆,我仍然不确定为什么带有大写字母的 URL 会导致重定向循环。
>>> from django.core.validators import URLValidator
>>> u = URLValidator(verify_exists=True)
>>> u.__call__('http://www.nacolmeia.com.br/do/Home/oferta/EnER')
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/dev/.virtualenvs/thepidb/lib/python2.7/site-packages/django/core/validators.py", line 105, in __call__
raise broken_error
ValidationError: [u'This URL appears to be a broken link.']
>>> u.__call__('http://www.nacolmeia.com.br/do/home/oferta/ener')
>>>
实际引发的异常是 HTTPError:
File "/usr/lib/python2.7/urllib2.py", line 606, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "/usr/lib/python2.7/urllib2.py", line 398, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 511, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 430, in error
result = self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 370, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 606, in http_error_302
return self.parent.open(new, timeout=req.timeout)
File "/usr/lib/python2.7/urllib2.py", line 398, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 511, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 430, in error
result = self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 370, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 596, in http_error_302
self.inf_msg + msg, headers, fp)
HTTPError: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop.
The last 30x error message was:
Found
>>>
以下是一些讨论 HTTPError 的帖子:here http://bytes.com/topic/python/answers/27369-urllib2-upgrade-breaking-urlopen and here http://www.gossamer-threads.com/lists/python/bugs/888877
似乎它与cookie有关,但我无法提供一个很好的解释,我将把它留给其他人。
如果您不想关闭验证但不关心网址的大写,则可能有效的解决方法是覆盖表单的 clean_field 方法。
def clean_your_url_field(self):
return self.cleaned_data['your_url_field'].lower()