gpt4 book ai didi

django 在/'file' 处引发 MultiValueDictKeyError

转载 作者:行者123 更新时间:2023-12-04 17:32:56 28 4
gpt4 key购买 nike

我正在尝试创建一个用于上传 .csv 文件并在模板上解析它的 View ,但它返回一个

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 validate" type="text">
</div>
<button class="waves-effect waves-light btn teal" type="submit">Upload</button>
</div>
</form>

那我的看法有

import csv, iofrom django.shortcuts import renderfrom django.contrib import messagesdef data_upload(request):    template = "home.html"    if request.method == 'GET':        return render(request, template)    csv_file = request.FILES['file']    if not csv_file.name.endswith('.csv'):        messages.error(request, 'Please upload a .csv file.')    data_set = csv_file.read().decode('UTF-8')    io_string = io.StringIO(data_set)    next(io_string)    for column in csv.reader(io_string, delimiter=',', quoteschar="|"):        _, created = Table.objects.update_or_create(            page=column[0],             keyword=column[1],             interval=column[2],             email=column[3],             billing=column[4],             notes=column[5],        )    context = {}    return render(request, template, context)

我想知道为什么它会返回错误(?)。

最佳答案

我加了

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

运行成功

关于django 在/'file' 处引发 MultiValueDictKeyError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57899513/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com