'WSGIRequest' 对象没有属性 'FILE'

ott*_*omd 5 python django excel file

我想制作一个应用程序,它接受 excel 文件并读取其内容,而不使用表单和模型。

我不断收到标题中的错误,我想知道错误是什么。

这是我的 HTML:

<div style="width: 800px; margin: 0 auto;">
    <form enctype="multipart/form-data"  action="." method='POST'> {% csrf_token %}
        <input type="file" name="excelfile">
        <input type="submit" value="Submit" />
    </form>
</div> 
Run Code Online (Sandbox Code Playgroud)

这是我的观点:

def uploadexcelfile(request):
    if request.method == "POST":
        excel = request.FILE['excelfile'].read()

        print('Opening workbook...')

        wb = openpyxl.load_workbook(excel)
        activesheet = wb.active
        print(activesheet.title)

        sheet = wb.get_sheet_by_name(activesheet.title)

        print('Reading rows...')
        for row in range(1, sheet.max_row + 1):
            url = sheet['A' + str(row)].value

            print(url)
    else:
        return render(request, 'uploadexcelfile.html')
Run Code Online (Sandbox Code Playgroud)

nik*_*k_m 8

我认为你正在寻找request.FILES而不是request.FILE.