I'm using the Django URLValidator
in the following way in a form:
def clean_url(self):
validate = URLValidator(verify_exists=True)
url = self.cleaned_data.get('url')
try:
logger.info(url)
validate(url)
except ValidationError, e:
logger.info(e)
raise forms.ValidationError("That website does not exist. Please try again.")
return self.cleaned_data.get('url')
It seems to work with some url's but for some valid ones, it fails. I was able to check with http://www.amazon.com/ it's failing (which is obviously incorrect). It passes with http://www.cisco.com/. Is there any reason for the bogus errors?
Look at the source for
URLValidator
; if you specifycheck_exists
, it makes aHEAD
request to the URL to check if it's valid:Try making the
HEAD
request to Amazon yourself, and you'll see the problem:I can't see a way of solving this other than monkey-patching or otherwise extending
URLValidator
to use aGET
orPOST
request; before doing so, you should think carefully about whether to usecheck_exists
at all (without which this problem should go away). Ascore/validators.py
itself says,You'll find that the in-development version of Django has indeed disposed of this feature completely.