[Fixed]-Delete a Picture & thumbnails with sorl.thumbnail (Django)

1πŸ‘

βœ…

It seems that you are trying to use the delete method from sorl to delete an instance of the Picture model instead of the picture itself.

This code should work :

from sorl.thumbnail import delete

def deletepicnthumbs(request, pk):
    pictodelete = Picture.objects.get_object_or_404(id=pk, user=request.user)
    # Delete the thumbnails, as well as the original image
    # If you want to keep the original image, pass ```delete_file=False```
    delete(pictodelete.file)
    # We use Django's method to delete the Picture instance, if needed
    pictodelete.delete()
    return redirect(adddetails)

Hope this helps,
Adela

Leave a comment