Empty Request.FILES with Django Upload forms

Solution 1:

old question, but somebody might still find this useful.

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

<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
{{ form.as_p }}
<button type="submit">{% trans 'Submit' %}</button>
</form>

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.

Solution 2:

It seems as request.FILES is not necessary in this case (good thing cause it's empty ...)

I modified this line

wayfinder_map.media_file = request.FILES['media_file'] 

for

wayfinder_map.media_file = wayfinder_map_form.cleaned_data['media_file'] 

and it works. Not sure what the right way to do thing though... –