SOLVED: django raises MultiValueDictKeyError at / 'file'

twitter logo github logo Updated on ・1 min read

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
twitter logo DISCUSS (7)
markdown guide
 

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"

 

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!

 

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

I added


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

and it was able to load the page

Thanks!

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

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

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

Classic DEV Post from Mar 9

What’s an unpopular software opinion you have?

Please share! ...

highcenbug🇵🇭 profile image
Freelance Developer | Volunteer Developer at Project: Website

dev.to now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️