Reputation: 141
I'm trying to make simple form run but I have cost, I'm not using the forms.forms of django, I'm doing the forms from HTML directly.
Well, my form simply has 1 file field and a post button, in the field I want to add an .xlsx and get data from that file and register it in a specific model but it gives me the following error:
django.utils.datastructures.MultiValueDictKeyError: "'ar'"
acontinuacion I want to show you how I have structured my code and I do not understand why I get an error, I would also like to give me an idea of how to obtain the excel data that I select in the input file and register it in bd:
View:
from django.views.generic import View
from django.shortcuts import render,redirect
from condominio.models import *
class TestExcel(View):
def post (self, request, *args, **kwargs):
print (request.FILES)
file = request.FILES['ar']
return HttpResponse('this is post')
def get(self, request, *args, **kwargs):
return render(request ,'testing.html' ,{})
template :
{% extends 'base.html' %}
{% load staticfiles %}
{% load static %}
{% block content %}
<div class="container">
<div class="row">
<form action="#" method="POST">
{% csrf_token %}
<div class="file-field input-field">
<div class="btn">
<span>File</span>
<input name="ar" id = "ar" type="file" >
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text" >
</div>
</div>
<button class="btn waves-effect waves-light" type="submit" name="action">Submit
<i class="material-icons right">send</i>
</button>
</form>
</div>
</div>
{% endblock %}
Upvotes: 0
Views: 1249
Reputation: 106425
You should add the enctype="multipart/form-data"
attribute to your form
tag:
<form action="#" method="POST" enctype="multipart/form-data">
Excerpt from Django's documentation:
Note that request.FILES will only contain data if the request method was POST and the that posted the request has the attribute enctype="multipart/form-data". Otherwise, request.FILES will be empty.
Upvotes: 2