0

I am developing an application with django for uploading a file on the server. I have defined a form and a model in forms.py and models.py files separately as below(respectively):

from django import forms

class DocumentForm(forms.Form):
    docfile = forms.FileField(
        label=''
    )

and in models.py:

from django.db import models

# Create your models here.

    class Document(models.Model):
        docfile = models.FileField(upload_to='targetdir')

in my HTML file and my form is:

    <form  class="myclass" action="submit" method="post">
    {% csrf_token %}

    <p>
                {{ form.docfile.errors }}
                {{ form.docfile }}
</p>


        <br />
            <input font-size="50px" style="zoom:1.5"  class="myclass" dir="rtl" type="submit" value="upload"  id="button" class="top-menu" onclick="pythonhandler()" />

now, whenever I submit my form and I wanna to receive my uploaded file on the server via below codes, I got "

raise MultiValueDictKeyError(repr(key))
MultiValueDictKeyError: "'docfile'""

error. my views.py file:

def pythonhandler(request):

    if request.method == 'POST':
        try:
                    data = request.FILES.get('docfile') 
                    with open(os.getcwd()+'/mydirectory/'+request.FILES['docfile'].name, 'wb+') as destination:
                        for chunk in request.FILES['docfile'].chunks():
                            destination.write(chunk)

I did the mentioned steps in this , this and this question, but I receive this error again!

4
  • 1
    Please can you be more specific about your html form and views.py? Did you read this: docs.djangoproject.com/en/1.11/topics/http/file-uploads? Maybe it could be useful. Commented May 3, 2017 at 14:48
  • I edited my question. I have read your mentioned link and every thing I think is such as that's example. but I have no idea about the error. Commented May 3, 2017 at 15:14
  • 1
    Note that request.FILES will only contain data if the request method was POST and the <form> that posted the request has the attribute enctype="multipart/form-data". So you miss it in your form tag Commented May 3, 2017 at 15:15
  • I setted enctype="multipart/form-data in my form and now every thing is ok! Commented May 3, 2017 at 15:21

2 Answers 2

1

in your view function

def pythonhandler(request):
data = DocumentForm(request.POST, request.FILES)

and in your html file

<form  class="myclass" action="submit" enctype="multipart/form-data" method="post">
    {% csrf_token %}

    <p>
                {{ form.docfile.errors }}
                {{ form.docfile }}
</p>
            <input type="submit" value="upload" id="button" class="top-menu" onclick="pythonhandler()" />
Sign up to request clarification or add additional context in comments.

2 Comments

The problem was due to enctype="multipart/form-data" attribute in my form. the View function is ok.
jsut gave a smaller way. anyways pleasure that it helped
0

I have missed enctype="multipart/form-data" command in my form tag in my HTML file. So, my form in the HTML file must be such as bellow:

<form  class="myclass" action="submit" enctype="multipart/form-data" method="post">
    {% csrf_token %}

    <p>
                {{ form.docfile.errors }}
                {{ form.docfile }}
</p>
            <input type="submit" value="upload" id="button" class="top-menu" onclick="pythonhandler()" />

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.