1

I'm updating my profile image, the image is updated and the previous image is deleted from database, but I want to delete it from the folder.

forms.py

def update(self, user):
    #import pdb; pdb.set_trace()
    data= self.cleaned_data
    kuser = KUser.objects.filter(user=user)
    if kuser:
        kuser = kuser[0]
        # delete file and update photo code here
        if kuser.profile_image.image:
            old_image = PHOTO_UPLOAD_DIR + kuser.profile_image.image.url
            if  os.path.exists(old_image):
                os.remove(old_image)
                os.remove(kuser.profile_image.image.url)
        kuser.profile_image.image = data['image']
        kuser.profile_image.save()
    return kuser

Image is deleted in database but not from folder.

Please, can anyone tell how to do this?

cor
  • 3,323
  • 25
  • 46
Gurjeet Singh
  • 103
  • 2
  • 6
  • check out this answer http://stackoverflow.com/questions/5372934/how-do-i-get-django-admin-to-delete-files-when-i-remove-an-object-from-the-datab – Andrii Zarubin Sep 16 '14 at 08:44

0 Answers0