19

I have already consulted lots of forums and I can't get an answer. I have installed a file upload in my Django app to save data into my server. But it does not work. Instead, it raises a MultiValueDictKeyError. I guess the problem is that there is not request.FILES (because it raises an error in request.FILES mentions), so the file upload is not working. This is my views.py:

def list_files(request, phase_id):
    phase = get_object_or_404(Phase, pk=int(phase_id))  
    if request.method == 'POST':
    #form = DocumentForm(request.POST, request.FILES)
    form = DocumentForm(request.POST, request.FILES)
    if form.is_valid():
        newdoc = Document(docfile = request.FILES['docfile'], phase = phase_id)
        newdoc.save()
        doc_to_save = request.FILES['docfile']
        filename = doc_to_save._get_name()
        fd = open(settings.MEDIA_URL+'documents/'+str(filename),'wb')
        for chunk in doc_to_save.chunks():
            fd.write(chunk)
        fd.close()

        return HttpResponseRedirect(reverse('list_files')) 
    else:
        form = DocumentForm()

    documents = Document.objects.filter(phase=phase_id)

    return render_to_response('teams_test/list_files.html',{'documents': documents, 'form':form, 'phase':phase}, context_instance = RequestContext(request)
    )

The document form in forms.py:

class DocumentForm(forms.ModelForm):
    docfile = forms.FileField(label='Select a file', help_text='max. 42 megabytes')
    class Meta:
    model = Document

The class document in models.py:

class Document(models.Model):
    docfile = models.FileField(upload_to='documents')
    phase = models.ForeignKey(Phase)

Finally, my html code:

{% extends "layouts/app.html" %}
{% load i18n  user %}

{% block title %}{% trans "Files list" %}{% endblock %}
{% block robots %}noindex,nofollow{% endblock %}


{% block page%}

<div id="page" class="container">
    <div class="header prepend-2 span-20 append-2 last whiteboard">
        <h2 style="margin-left:-40px">{{ phase.name }} files</h2>

        {% if documents %}
        <ul>
        {% for document in documents %}
        <li><a href="{{ document.docfile.url }}">{{ document.docfile.name }}
        {% endfor %}
        </ul>
    {% else %}
        <p>No documents.</p>
    {% endif %}

        <form action="{% url list_files phase.id %}" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <input id="file" type="file" />
        <input id="submit" type="submit" value="Upload file" />
        </form>
  </div> 
</div>
{% endblock %}

My traceback says:

Exception Type: MultiValueDictKeyError
Exception Value:    "Key 'docfile' not found in <MultiValueDict: {}>"
my_dir/views.py in list_files
    newdoc = Document(docfile = request.FILES['docfile'], phase = phase_id) 

And my QueryDict is empty:

POST:<QueryDict: {u'csrfmiddlewaretoken': [u'UZSwiLaJ78PqSjwSlh3srGReICzTEWY1']}>

Why? What am I doing wrong?

Thanks in advance.

4 Answers 4

20

You need to change multipart/form_data to multipart/form-data - that's why request.FILES is empty: the form isn't sending things in the way Django expects due to the typo. [EDIT: this has now been done]

Update 1: Also, rather than directly access request.FILES, try relying on the modelform's default behaviour, as then it'll have been handled as an upload appropriately. ie, newdoc = form.save() should do all you need, from a quick look at it - is there a particular reason you manually saving the file when the modelform can do that for you?

Update 2: Ah, look: you're not assigning a name to your file upload element

From the docs:

HttpRequest.FILES A dictionary-like object containing all uploaded files. Each key in FILES is the name from the <input type="file" name="" />. Each value in FILES is an UploadedFile

So, you need to change

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

to

or, for default Django convention

<input id="id_docfile" type="file" name="docfile"/>

Indeed, it's usually better to use the Django form to render the actual field, even if you've moved beyond the whole {{form.as_p}} approach:

{{form.docfile}}

PS. if you've not read them fully, I heartily recommend taking the time to go through all of the forms documentation

6
  • Updated, with another suggestion, after you changed to form-data Apr 3, 2014 at 9:01
  • No particular reason, I was just testing different options to solve the problem. However, if the problem is in request.FILES, shouldn't be any error in the transfer of the file? The form in html or something. I don't know, but any data arrives to my views.py.
    – DavidRguez
    Apr 3, 2014 at 9:07
  • I have changed my code including form.save(), but there is no changes. But there is something weird here: the error is being throwing by a comment. In my traceback: my_dir/views.py in list_files #newdoc = Document(docfile = request.FILES['docfile'], phase = phase_id) ... /home/lince/teams/env/lib/python2.6/site-packages/django/utils/datastructures.py in __getitem__ raise MultiValueDictKeyError("Key %r not found in %r" % (key, self)) ... How is this possible?
    – DavidRguez
    Apr 3, 2014 at 9:11
  • My mistake, this last thing is solved (the error of the comment). When I do: if form.is_valid() it throws False. Where is the origin of the problem?
    – DavidRguez
    Apr 3, 2014 at 9:19
  • Updated again - not related to you form.is_valid query specifically, but should also solve that. Apr 3, 2014 at 9:21
11

Modify Post method to

<form action="" method="post" enctype="multipart/form-data">{% csrf_token %}
2
  • Why do you expect this to help (and what are you changing? Just making the action point to self versus what the OP was pointing at? Fixing enctype was the first thing suggested by the accepted answer)
    – Foon
    Oct 7, 2015 at 3:07
  • This was all I needed to add. May 26, 2019 at 12:22
0

For anymore who tried the above and still couldn't figure out a solution. Here's what I did:

views.py

if request.method == 'POST':
    doc = request.FILES #returns a dict-like object
    doc_name = doc['filename']
    ...
0

For anyone who tried the above and still couldn't figure out a solution. Here's what I did (2nd part):

if request.method == 'POST' and 'filename' in request.FILES:
    doc = request.FILES #returns a dict-like object
    doc_name = doc['filename']
    ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.