python - “WSGIRequest”对象没有属性 'FILE'

标签 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> 

这是我的观点:

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')

最佳答案

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

关于python - “WSGIRequest”对象没有属性 'FILE',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45839867/

相关文章:

python - Cython:创建扩展类型时是否必须手动实现 __getattr__ 方法?

python - 多处理速度与内核数量的关系

python - 如何使用python将图像插入特定的excel文件单元格?

java - 无法使用 POI 在 Excel 中生成下拉菜单

excel - 使用 VBA 取消隐藏 Excel 中的行

python - 如何在python中的数据框中按行查找值的索引并提取以下列的值

python - OSX Mavericks 破坏了 pip 和 virtualenv

django - 在 Django 中有几个名字和一个主要名字的模型人?

django - 我在哪里为 Django 安装 Twitter Bootstrap - 主文件夹或静态文件夹?

python - 无法覆盖 django 中 NumberInput 字段的步骤属性