Hi, I am new in django and I am having problems with the removal of a file when a user uploads a new one (the old file is no longer useful for the app). The thing is that when I user wants to update his/her profile picture, my application should remove the old one. I tried this by overriding the save function in my form UserProfileForm, related to the UserProfile model. I have written the same lines of the save function in the shell console and it seems to work. I also tried to override the save function in the model but I got the same negative result. I would really appreciate if anyone could give me some advice about what I am doing wrong and how should I fix it! ;) Thank you in advance, Mario PS: I am using the django-profiles app. It calls the save function of the form_class, which is actually my UserProfileForm. =) One portion of my model
My form for the model
Finally the function handle_uploaded_file, which actually works fine! ;) from PIL import Image as PImage from os.path import join as pjoin from django.conf import settings
asked 15 Nov '10, 11:48 mariocao |
Since it is simply failing to delete the image and is not throwing an error, then I suspect that your answered 15 Nov '10, 12:55 seanf |
Hi, It was true what you said, but the unexpected problem (for me) was while creating a form object in my view with the following line:
This line made that the image field of my model already points to the new uploaded image. Therefore I could not delete the old one (the path to it was nowhere). My solution was a workaround! ;) I deleted the old uploaded file by using the delete() function of the ImageField object. Then a saved the form and finally I resized my uplaoded image! ;) Anyway, thank you very much for your help!!! ;)
answered 16 Nov '10, 04:30 mariocao |
You say that it seems to work in the shell. What happens on your site? Do you get any kind of error message, or does it fail silently (ie, it simply fails to delete the old image)?
It simply fails to delete the old image. I have not said it, but if a set "image_path" to a fixed path, e.g. "/home/username/webapss/static_media_app/profile_images/file.png" it deletes my images. It is really strange, but perhaps just something I have overread or forgotten. =S
Thanks for your interest and help! ;)