On 11 ?.?. 2010, at 11:00, jimgardener wrote:

> hi
> In my application I am taking user given url strings.The user is
> likely to enter 'www.google.com' instead of 'http://
> www.google.com' .When I try to open the url
> 
> import urllib2
> page=urllib2.urlopen(url)
> 
> can give a ValueError if the user fails to enter 'http'.How can this
> problem be addressed?

I think the most 'correct' way to do this would be to use a URLField in your 
form so that form.is_valid() will force the user to enter the http:// 
themselves. (Of course a message to the user on the form explaining this would 
be helpful.) 

This will also have nice side-effects like checking that it ends in a valid 
TLD, and if you set URLField.verify_exists=True it will even ensure that the 
URL leads somewhere other than a 404.

> do I have to prepend 'http' to every urlstring
> before trying to open it using urlopen?

Otherwise yes, I'd use something like:

if not url.startswith('http://'):
        url = 'http://' + url

I'm a Django newbie myself, however, so someone else may chime in with a better 
option for you.

Good luck!
Jonathan

> thanks
> jim
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to 
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/django-users?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to