1

In my models.py

class Post(models.Model):
    url = models.URLField(max_length=250, blank=True, null=True)

    @property
    def domain(self):
        long_url = urlparse(self.url).netloc if self.url else "hello"
        return long_url.split('.', 1)[1] if long_url.split('.', 1)[0] == 'www' else long_url

but I've decided to remove "hello" so I changed to None.

Now I get the error:

'NoneType' object has no attribute 'split'

any help please...

4
  • None stored inside long_url Commented Mar 10, 2016 at 6:09
  • long_url is None Commented Mar 10, 2016 at 6:10
  • @danidee yes but how do I not display anything if it's not self.url Commented Mar 10, 2016 at 6:14
  • @AvinashRaj yes but how do I not display anything if it's not self.url Commented Mar 10, 2016 at 6:15

1 Answer 1

1

Your long_url can be null since your model accepts it to be so. Add a null check before doing the split.

return long_url.split('.', 1)[1] if (long_url and long_url.split('.', 1)[0] == 'www') else long_url

should work.

Sign up to request clarification or add additional context in comments.

8 Comments

so how do I solve this issue? I can see long_url can be null if I use None but isn\t there a way to make it None??
My Code will return None if long_url is None. I believe that's what you want?
with this code I get (None), I don't want anything to be displayed if it's not self.url
sorry my goal was to not display it at all
What do you mean by display? Returning None from a function is equivalent to an empty line (you can handle this in the view maybe?) If you want to return a string, just initialize long_url = "" instead of None in the first line. Eg :
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.