Milano
Milano

Reputation: 18705

Uploading a file does not work properly in Django

I'm just starting to work with Django-Multiupload.

I have a problem - when I try to upload file/files, it seems that it works but no file is being uploaded in fact.

VIEWS.PY

def index(request):
    multiple_file_upload_form = MultipleFileUploadForm()

    if request.method == 'POST':
         print request.FILES 

    return render(request,'index.html',
                  context={'file_upload_form':multiple_file_upload_form})

INDEX.HTML

<form action="" method="post"> {% csrf_token %}
       {{ multiple_file_upload_form | crispy }}
       <input type="submit" value="submit">
</form>

FORMS.PY

class MultipleFileUploadForm(forms.Form):
    attachments = MultiFileField(min_num=1)

After choosing some file from my PC and clicking on submit, there is <MultiValueDict: {}> in cmd.

Do you know why?

EDIT:

After changing view code:

if request.method == 'POST':
        if multiple_file_upload_form.is_valid():
            print 'ok'
            print request.FILES
        else:
            print multiple_file_upload_form.errors

It print's that field attachements is required. But I've fill it (file has been chosen).

Upvotes: 1

Views: 69

Answers (1)

Adriano Silva
Adriano Silva

Reputation: 2576

Include enctype in your form: http://www.w3schools.com/tags/att_form_enctype.asp

<form action="" method="post" enctype="multipart/form-data">

Upvotes: 2

Related Questions