Empty Request.FILES with Django Upload forms

django upload file without form
django request.files empty
django read uploaded file
django upload image
django multiple files upload using ajax
django upload zip file
file upload and download in django
django-crispy-forms file upload

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 bundled dev server.

Been stuck here and went through all related questions.

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

generates

MultiValueDictKeyError: "Key 'media_file' not found in MultiValueDict: {}>"

model

class WayfinderMap(models.Model):
    """ Way-finding Map Config"""


    media_file = models.FileField(upload_to="maps", null=True, blank=True) 
    wall_file = models.FileField(upload_to="maps_data", null=True, blank=True) 

view

@login_required
def create_map(request, form_class=WayfinderMapForm, template_name="wayfinder/map/create.html"):
wayfinder_map_form = form_class(request.user, request.POST or None, request.FILES)

    if wayfinder_map_form.is_valid():
        wayfinder_map = wayfinder_map_form.save(commit=False)
        wayfinder_map.media_file = request.FILES['media_file']
        wayfinder_map.data_file = request.FILES['data_file']
        wayfinder_map.creator = request.user
        wayfinder_map.save()
    return HttpResponseRedirect(wayfinder_map.get_absolute_url())

return render_to_response(template_name, {
    "wayfinder_map_form": wayfinder_map_form,
}, context_instance=RequestContext(request))

template

<form enctype="multipart/form-data" class="uniForm" id="wayfinder_map_form" method="POST" action="">
        <fieldset class="inlineLabels">
            {{ wayfinder_map_form|as_uni_form }}
            <div class="form_block">
                <input type="hidden" name="action" value="create" />
                <input type="submit" value="{% trans 'create' %}"/>
            </div>
        </fieldset>
    </form>

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.

File Uploads | Django documentation, But the file actually doesn't upload since `form.is_valid()` evaluates to false because file is a `required` field. My forms.py file is: class EmployeeTestForm  (Working with django 1.6, python 3.3) I've set encryption type and other thing, but still my request.FILES is empty. – Alireza Farahani Jan 15 '14 at 22:24 @alireza, even thou SO is not a "support" site you might try and paste the specifics of your configuration, or a link to code if OS, otherwise it's too difficult to guess what your problem

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... –

request.FILES is empty. File didn't upload : django, Dear All, I'm just starting with django, and I'm working on a simple book And I recommend you to watch Vitor video on file upload, in that you  The problem is that request.FILES is always empty. But when I try to print request.body it shows some non-human-readable data which particularly include the data from the file I want to upload (it's a database).

I think your troubles may lie in assigning data to a form without first verifying the the request is POST

@login_required
def create_map(request, form_class=WayfinderMapForm, template_name="wayfinder/map create.html"):
  if request.method=='POST':
    wayfinder_map_form = form_class(request.user, data=request.POST, files=request.FILES)

    if wayfinder_map_form.is_valid():
      #save your data
      return HttpResponseRedirect(wayfinder_map.get_absolute_url())

  else:
    wayfinder_map_form=form_class(request.user)

 return render_to_response(template_name, {"wayfinder_map_form": wayfinder_map_form,}, context_instance=RequestContext(request))

Modal Forms generate empty request.FILES - Questions, Introduction - Django File Upload Tutorial - Part 1 request.FILES will be empty. The form FileField and ImageField are created as a string field in the database (usually VARCHAR), containing the reference to the actual file. Django file upload using Form. Ask Question I was trying to upload an empty file, my dumb mistake, but question, when i try to upload the same file to a table

I found that If you put put the form in a content block then it seems to work

    {% block content %}
<form enctype="multipart/form-data" class="uniForm" id="wayfinder_map_form" method="POST" action="">
        <fieldset class="inlineLabels">
            {{ wayfinder_map_form|as_uni_form }}
            <div class="form_block">
                <input type="hidden" name="action" value="create" />
                <input type="submit" value="{% trans 'create' %}"/>
            </div>
        </fieldset>
    </form>
{% endblock %}

How to Upload Files With Django, a file to upload Django always complains about the field being empty and my form never validates. Here's my model: class IOs(models.Model): from django import forms class UploadFileForm(forms.Form): title = forms.CharField(max_length=50) file = forms.FileField() A view handling this form will receive the file data in request.FILES, which is a dictionary containing a key for each FileField (or ImageField, or other FileField subclass) in the form.

request.FILES always empty, I have sucessfully created a view and form to upload an image, however, when I want to edit that image request.FILES is empty and I get an  Django comes with two form fields to upload files to the server, FileField and ImageField, the following is an example of using these two fields in our form forms.py: from django import forms class UploadDocumentForm(forms.Form): file = forms.FileField() image = forms.ImageField()

Help with Django Request.Files and form fields, FILES will be empty. The form must be submitted using the POST method. Django have proper model fields to handle uploaded files: FileField and ImageField. # forms.py from django import forms class UploadFileForm(forms.Form): title = forms.CharField(max_length=50) file = forms.FileField() #views.py from django.http import HttpResponseRedirect from django.shortcuts import render from .forms import UploadFileForm # Imaginary function to handle an uploaded file.

How to process file upload in HTML forms using Python Django , Using a Content-Type of multipart/form-data yields an error: I write a upload-file program with the lastest sanic(install from master branch),  Otherwise the request.FILES will be empty. The form must be submitted using the POST method. Django have proper model fields to handle uploaded files: FileField and ImageField .

Comments
  • i think the answers to this similar question will help you out : stackoverflow.com/q/5895588/1037459
  • @Stefano: I've fallen on this issue too. (Working with django 1.6, python 3.3) I've set encryption type and other thing, but still my request.FILES is empty.
  • @alireza, even thou SO is not a "support" site you might try and paste the specifics of your configuration, or a link to code if OS, otherwise it's too difficult to guess what your problem is...
  • when you are so much blinded by the framework, simple things like this one is always forgotten. Thank you!
  • jQuery Mobile also requires: data-ajax="false"
  • Note that request.raw_post_data is now request.body as of 1.6
  • been using it this way with no problem so far. might be a good check (if necessary) but it certainly does not solve the problem as request.FILES is still empty
  • What do you mean 'no problems so far'? Did this work properly before? Can you post your form class?
  • What I mean is that I've been using forms with no problem, as long as I don't have to use request.FILES, without checking if the method is POST. Does tying request.POST to data in the instantiation of the class like you do could help ? I'll try that and post my form.