2

I need to check whether the given url is valid or not in django. I have used URLValidator for checking the url in django. But it's not working. I have explained the code below.

 from django.core.validators import URLValidator
 from django.core.exceptions import ValidationError
 a = "www.google.com"
 url = URLValidator()
 try:
    url(a)
    print "correct"
 except:
    print "wrong"

When i tried to run the above concept it's showing the output "wrong". And then when i passed the value of a="http://www.google.com" means that the output is "correct". After that when i passed the value of a="http://www.googlecom" means that the output is "correct" .am more confused with it. Please anyone help me to do this concept.

Thanks in advance.

Python Team
  • 1,143
  • 4
  • 21
  • 50
  • 1
    I think this issue was discussed here? http://stackoverflow.com/questions/7160737/python-how-to-validate-a-url-in-python-malformed-or-not – Yousef_Shamshoum May 13 '15 at 06:26

1 Answers1

2

"www.google.com" is not valid URL because there is no protocol (http or https for example). It is malformed.

"http://www.googlecom" is treated as a valid url by Django because it has the right format (protocol://something.something). Although this url does not work, it is not malformed.

...Django does not try to check if googlecom is a valid Top-Level Domain because there are lots of those and the set is changing often. It only checks that the URL is not malformed.

Anentropic
  • 32,188
  • 12
  • 99
  • 147