[Fixed]-Request.FILES always empty on file upload

6๐Ÿ‘

โœ…

I hope that you have already solved this issue.
I had the exactly same issue and I found out I had no name prop in input tag

<input type="file" id="grade_csv" /> that is your input.

if it doesnโ€™t have name, django wonโ€™t take it.
So add name prop then it will work well.

29๐Ÿ‘

Check that you have added enctype property into form tag.

Example from official docs:
<form enctype="multipart/form-data" method="post" action="/foo/">

2๐Ÿ‘

I had this problem too and was puzzling over it for a while. My error was the same โ€” that I was missing necessary fields on the input field.

In order to see what the actually required fields are, a very nice thing to do is, in this case:

from base.forms import DocumentForm ## or put whatever the name is of the app, which is unspecified in the question
print DocumentForm()

This will print the html that you need, including all the tags that this object requires. Very neat functionality which I missed the first time, but which is outlined in the docs.

๐Ÿ‘คgabe

1๐Ÿ‘

<input type="file" id="grade_csv" name="name" />

You have to add name to your input field.

๐Ÿ‘คaksh_sharma

1๐Ÿ‘

Two things to make sure of here:

  1. Form tag should contain attribute "enctype="multipart/form-data"
  2. Each file field should have a "name" attribute
๐Ÿ‘คAchal Parikh

0๐Ÿ‘

I just encountered a weird bug where it shows as empty when I print it out:

print(request.FILES)
# Prints <MultiValueDict: {}>

If I load up a debugger and actually inspect the object though, itโ€™s not empty and contains the data I was expecting. This is the same exact object in both cases. The debugger shows FILES as populated, then when I continue, print shows it as empty.

So, if youโ€™re like me and you wanted to poke at the object first to understand it a bit, maybe donโ€™t rely on MultiValueDictโ€˜s string representation. I canโ€™t explain the source of the bug though since MultiValueDict is just subclass of dict and uses dictโ€˜s __str__.

๐Ÿ‘คCarcigenicate

Leave a comment