Empty Request.FILES with Django Upload forms

后端 未结 5 699
滥情空心
滥情空心 2020-12-08 02:03

Trying to use a very simple form to upload a file into a new class instance. I am expecting to have both files in request.FILES but it\'s empty. I am on the bun

5条回答
  •  佛祖请我去吃肉
    2020-12-08 02:27

    old question, but somebody might still find this useful.

    In order to have your files uploaded and showns in request.FILES, your form MUST contain enctype="multipart/form-data", eg:

    {% csrf_token %} {{ form.as_p }}

    otherwise your files will not be uploaded and your request.FILES will be empty.

    BTW That's a common solution to a common error. Nevertheless, I still found myself in a situation with an empty FILES (and the file in the POST) when everything else was looking OK. I have the feeling it was a size limit but did not want to spend more time debugging, and just used request.raw_post_data. If ever somebody falls on this issue please add a comment (including precise django version!) and I'll try to debug more deeply.

提交回复
热议问题