我正在尝试使用上传的xlsx文件中的数据填充数据库。
我已按照本教程中的步骤操作:https://buildmedia.readthedocs.org/media/pdf/django-excel/latest/django-excel.pdf
在我的情况下,上传后出现404错误页面,而Web服务器错误日志文件仅显示:
[Sun Mar 15 20:31:11.075408 2020] [wsgi:error] [pid 6756] [remote 192.168.1.7:53883] Bad Request: /Project/store/
这时我还是有点卡住了。。。我知道那个表格是无效的,但是为什么呢?
从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"
从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', {})
从模板中提取-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文件数据
code | name | address | city | zip
1 | Store01 | 191-st, Main street | Calhounn | 7000
2 | Store02 | 277-th, River streetGaleria | Verdounne | 9000
答案 0 :(得分:0)
您必须使用“文件”作为html中输入元素的名称。