I am trying to populate the database with data from uploaded xlsx file.
I have followed the steps from this tutorial: https://buildmedia.readthedocs.org/media/pdf/django-excel/latest/django-excel.pdf
In my case after upload the 404 error page occurs while the web server error log file only shows:
[Sun Mar 15 20:31:11.075408 2020] [wsgi:error] [pid 6756] [remote 192.168.1.7:53883] Bad Request: /Project/store/
I am somehow stuck at this point ... I u/stand that thew form is not valid, but why?
extract from models.py
class Store(models.Model):
code = models.CharField(max_length=100, unique=True)
name = models.TextField()
address = models.TextField()
city = models.CharField(max_length=100)
zip = models.IntegerField()
def __str__(self):
return self.cod+" "+self.nume
class Meta:
ordering = ["cod"]
verbose_name_plural = "Stores"
extract from view.py
class UploadFileForm(forms.Form):
file = forms.FileField()
def store(request):
if request.method == "POST":
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
request.FILES['file'].save_to_database(
model=Store,
mapdict=['code', 'name', 'address', 'city', 'zip'])
return HttpResponse("OK")
else:
return HttpResponseBadRequest()
else:
return render(request, 'store.html', {})
extract from template - store.html
<form action="{% url "store" %}" method="post" enctype="multipart/form-data">
{% csrf_token %}
<div class="form-group mb-4 mt-3">
<label for="exampleFormControlFile1">Excel file ONLY !</label>
<input type="file" title="Upload excel file" name="excel_file"
class="form-control-file" id="exampleFormControlFile1" required="required">
</div>
<input type="submit" value="Upload" name="time" class="mt-4 mb-4 btn btn-primary">
</form>
xlsx file data
code | name | address | city | zip
1 | Store01 | 191-st, Main street | Calhounn | 7000
2 | Store02 | 277-th, River streetGaleria | Verdounne | 9000
You have to use "file" as the name of the input element in html.