gpt4 book ai didi

django - Django 的 URL 字段测试问题

转载 作者:行者123 更新时间:2023-12-04 11:43:56 25 4
gpt4 key购买 nike

有人可以澄清我为什么这个网址 http://www.nacolmeia.com.br/do/Home/oferta/EnER未被 URLField 的 Django 生成的表单接受?

:)

谢谢

最佳答案

您是否在尝试验证站点的同一台服务器上托管站点? docs

Note that when you're using the single-threaded development server, validating a URL being served by the same server will hang. This should not be a problem for multithreaded servers.



它看起来不像是表单级别的失败验证
>>> 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)

编辑:

在 django 源代码中进行了一些挖掘后 here 和一些与 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 here

似乎它与cookie有关,但我无法提供一个很好的解释,我会把它留给其他人。

如果您不想关闭验证但不关心 url 的大小写,一个可能有效的解决方法是覆盖表单的 clean_field 方法。
def clean_your_url_field(self):
return self.cleaned_data['your_url_field'].lower()

关于django - Django 的 URL 字段测试问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5589059/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com