Ad

Looks Like A Valid Python Syntax To Me But Gives Me Invalid Error Using In Django

- 1 answer

Maybe I'm wrong but this looks like a valid python code to me but I get invalid syntax....I'm trying to split url,removing www when domain is displayed. This is my code:

return urlparse(urlsplit(self.url)).netloc if self.url else "be kind to one another" 

but this is wrong... maybe I'm approaching wrong for implementing this function.. any help would be appreciated

Edit 1: Now I get 'SplitResult' object has no attribute 'find'

Ad

Answer

It looks like you just need to delete urlsplit function from your code and it will work as expected:

from urlparse import urlparse
url = "http://stackoverflow.com/questions/34446468/looks-like-a-valid-python-syntax-to-me-but-gives-me-invalid-error-using-in-djang"
long_url = urlparse(url).netloc if url else "be kind to one another"
long_url.split('.', 1)[1] if long_url.split('.', 1)[0] == 'www' else long_url
Ad
source: stackoverflow.com
Ad