[Answered ]-Django – File name '' includes path elements exception, isn't the name supposed to include the path relative to MEDIA_ROOT?

1👍

I encountered this problem yesterday just like you and I found a solution just a minute ago.

The problem was caused by the file name just as the Django debug mode tried to tell you, "File name ‘uploads/winter3.jpg’ includes path elements".

"uploads/winter3.jpg" is not a legal path name, so what we need to do is to remove "uploads/" from the name of the image, make the name to be "winter3.jpe" and that is really simple.

Here is the code of the solution for this problem

def make_thumbnail(self, image, size=(300, 200)):
    img = Image.open(image)
    img.convert('RGB')
    img.thumbnail(size)

    thumb_io = BytesIO()
    img.save(thumb_io, 'JPEG', qulity=85)

    # just add replace("uploads/","") after the image.name
    thumbnail = File(thumb_io, name=image.name.replace("uploads/",""))
    return thumbnail
👤John

Leave a comment