How to save image in the media path using urllib

Am following a tutorial and i cant get to pass this part … everything else works fine untill this part below

def save(self, force_insert=False, force_update=False, commit=True):

            image = super().save(commit=False)

            image_url = self.cleaned_data["url"]

            name = slugify(image.title)

            extension = image_url.rsplit(".", 1)[1].lower()

            image_name = f"{name}.{extension}"

            # download image from the given URL

            response = request.urlopen(image_url)

            image.image.save(image_name, ContentFile(response.read()), save=False)

            if commit:

                image.save()

            return image

the image was supposed to be downloaded and saved to the image field of my model

but it never get saved or created its always empty

I’m assuming you’re running this in a development environment using runserver (or runserver_plus). Are you getting any error messages or traceback in your console where this is running? Do you have any information as to whether or not it’s actually retrieving a file?

Yes sir am running this on Runserver_plus the get part is working getting the url and I have a urlfield in my model everything seems to work great on there with the javascript snippet that passes the get request to django…

What I know is the url is passed to request. Url. Open. After this I don’t know what’s going on I couldn’t even debug tried using print to check what’s in response but wasn’t working

Is the save method posted above part of a view, form, or model? What does the view look like?

Are you using the Python stdlib urllib module, or the requests package? If you’re using the urllib module, I would encourage you to take a look at requests. In general, I find it to be a whole lot easier to use & understand.

Aside from that, if you’re using urllib, then what you want to check after the urlopen call are the values of the response.url and response.code (pre 3.9) or response.status (3.9+) being returned from the call. That’ll give you a starting point for further research.