[Answered ]-MultiValueDictKeyError at /"" when uploading empty file

1👍

cf MultiValueDictKeyError in Django

You’re executing

                uploadfile = UploadFile(
                    image=self.request.FILES["upload_file"])

Either use try / except to catch the error,
or rely on .get(): self.request.FILES.get("upload_file", None)

You might want to check for None before calling UploadFile().


As written, this doesn’t make sense:

            if submitted_form.is_valid() and self.request.POST:
                uploadfile = UploadFile(
                    image=self.request.FILES["upload_file"])

            uploadfile.save()

That is, we conditionally define the uploadfile variable,
and then unconditionally call one of its methods.
That call won’t work if the variable is undefined.

👤J_H

Leave a comment