DEV Community

Vicente G. Reyes
Vicente G. Reyes

Posted on • Updated on

SOLVED: django raises MultiValueDictKeyError at / 'file'

I'm trying to create a view for uploading a .csv file and parsing it on the template but it's returning a

MultiValueDictKeyError at /

'file'

The template has


<form method="POST" enctype="multipart/form-data">{% csrf_token %}
<div class="file-field input-field">
<div class="btn">
<span>Upload a CSV FILE</span>
<input type="file" name="file">
</div>
<div class="file-path-wrapper">
 <input class="file-path

Top comments (8)

Collapse
 
mahendrachoudhary profile image
Mahendra Choudhary

In your view
def data_upload(request):
template = "home.html"
# add debugger here and check your params by which name your form #sending file .
csv_file = request.FILES['file'] # here you not getting name of file
.
.

you can do this also , i don't know it will solve this or not
Upload a CSV FILE
# change name from "file" to something #else like "csv_file"

Collapse
 
highcenburg profile image
Vicente G. Reyes

I'll try this. I'm on another project so I'll let you know in a few hours if it worked or not. Thanks Mahendra!

Collapse
 
mahendrachoudhary profile image
Mahendra Choudhary • Edited

yup , let me knoe if it work. if not let me know how you solved it ..-

Thread Thread
 
highcenburg profile image
Vicente G. Reyes • Edited

I added


if request.method == 'GET':
return render(request, template)

and it was able to load the page

Thanks!

Thread Thread
 
mahendrachoudhary profile image
Mahendra Choudhary

ok , but does csv file uploaded? In your form you sending POST request , then why if request.method == 'GET' returning true????

Thread Thread
 
highcenburg profile image
Vicente G. Reyes

Yes, it went inside the admin through the models.py

Thread Thread
 
highcenburg profile image
Vicente G. Reyes

the next problem is to iterate the tables from the csv file

Thread Thread
 
divyam55 profile image
Divyam55

Hey! I'm having the same exact problem,
Can you share how you solved it?
Also how you iterate the tables from the csv file