Thank you tomZ! My problems were pretty much fixed by this snippet:
http://www.djangosnippets.org/snippets/636/ It does exactly what i wanted with only a few glitches - does not add that checbox after saving the form when i have uploaded the picture and does not remove the delete checkbox when i have removed the image and saved. Guess the creator of this snippet can help me there. Alan On Aug 7, 10:20 pm, zayatzz <alan.kesselm...@gmail.com> wrote: > Ok it seems i either asked the wrong question or did not explain well > enough. > > My view has such line: > pform = ProfileForm(instance=profile) > > it creates the form instance and i thought that it also loads data > from database and sends it to form, so i can use this in form save > method. I do not know if i understand this right or wrong. Perhaps it > just does not work for ImageField, because if i do > path = self.instance.img.name > os.remove(path) > > in form save method i get following error: > OSError at /profile/edit/ > > (2, 'No such file or directory') > > Request Method: POST > Request URL: http://127.0.0.1/profile/edit/ > Exception Type: OSError > Exception Value: > > (2, 'No such file or directory') > Exception Location: /home/projects/tst/profile/forms.py in save, > line 24 > ---------- > bit more down on the samge page i can see that path is empty. > > So what to do, if i wish to read old files name from databse and > delete it in the forms save method? > > Do i need to pass the file name to form in a view too? > > In addition to that problem. What do i need to do if i want that > ImageField has another link for removing existing image right next to > browse button in a view. What do i need to write for that? > > Alan. > > On Aug 7, 9:04 am,zayatzz<alan.kesselm...@gmail.com> wrote: > > > Thank you! > > > Alan. > > > On Aug 6, 10:51 pm, Daniel Roseman <dan...@roseman.org.uk> wrote: > > > > On Aug 6, 8:36 pm,zayatzz<alan.kesselm...@gmail.com> wrote: > > > > > Hello > > > > > I am not entirely sure if i am asking the right question, but here is > > > > what am trying to do. > > > > > I am saving user mugshot with form and it works just fine. I rename > > > > the uploaded file to mugshot.[insert imagefile extension here], but i > > > > want to do more in form save method than just that. > > > > > 1) i want to check if the model, whos form im saving already has > > > > imagefile saved. I have this in view: > > > > pform = ProfileForm(instance=profile) > > > > > but this in form save: > > > > os.remove(settings.MEDIA_ROOT+self.instance.img) > > > > > returns me ImageFieldFile not the data that is saved in it... and i > > > > get typeerror: > > > > TypeError at /profile/edit/ > > > > > cannot concatenate 'str' and 'ImageFieldFile' objects > > > > > 2) if it exists, then remove it - can do it os.remove(), but the > > > > problem is similar to last point - i need to know the name of existing > > > > file, that is saved in model data > > > > 3) if the file has been removed or did not exist in the first place > > > > then i want to save different sizes of the file - thumbnail, bigger > > > > pic and so on. i do not need the examples for that. > > > > > Alan > > > > self.instance.img.name will give you the relative path from > > > MEDIA_ROOT. > > > See:http://docs.djangoproject.com/en/dev/ref/files/file/#django.core.file... > > > -- > > > DR. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---