0

When uploading files from a form, it keeps throwing a error that the destination could not be found. I think it's obvious, because I open() the path for the file where there is no path yet, so kind of a forward reference i guess. But it's how I read it in the Django docs.

def form_valid(self, form):
    for index, image in enumerate(self.request.FILES.items()):
        with open(os.path.join(settings.MEDIA_ROOT,'image', str(index), 'image.jpg')) as path:
            for chunk in image.chunks():
                path.write(chunk)

    return super().form_valid(form)

So nothing special, I just try to upload images from forms from the html. It throws this error:

Exception Type:     FileNotFoundError
Exception Value:    

[Errno 2] No such file or directory: '/path/to/my/project/media/image/0/image.jpg'
dnsko
  • 1,017
  • 4
  • 17
  • 29
  • 1
    You have to use `open` in binary write mode. The default is read. `open(filename, 'wb')` – Håken Lid Jun 01 '16 at 09:02
  • Yeah I have tried that too, but it did not work unfortunately – dnsko Jun 01 '16 at 09:05
  • 1
    This error is because you are trying to open a non existing file for reading. If 'wb' doesn't work, it's a different error. Like a non existing directory. `open` will not create a parent directory for your file. See this question: http://stackoverflow.com/q/23793987/1977847 – Håken Lid Jun 01 '16 at 09:07
  • Okay thanks alot. It does not make the parent directory like you said, so i'll add a function for that :) – dnsko Jun 01 '16 at 09:24

0 Answers0