TypeError: a bytes-like object is required, not 'str' when using urlparse

Zorgan Source

I'm getting this error on my model function. Here's my model:

def Post(models.Model):
    ...
    imageURL = models.URLField(null=True, blank=True)

    @property
    def video_source(self):
        print(self.imageURL) #https://www.youtube.com/watch?v=abcdefghi
        t = urlparse(self.imageURL).netloc #this line fires the error
        domain = '.'.join(t.split('.')[1:])
        print(domain)
        return True

I'm trying to create a function for my template like so:

{% if instance.imageURL.video_source %}
    #something
{% else %}
    #something else
{% endif %}

Any idea what the problem is?

pythondjangopython-3.x

Answers

answered 6 months ago Jon Reinhold #1

Change the URL to a byte data type.

url = url.encode()

comments powered by Disqus