我正在构建一个非常简单的应用程序,其中包括一个页面,用户提交一个邮政编码列表,然后返回一个邮政编码在原始列表的某个半径的文件.

我的问题是,我用于处理包含在views.py文件中的文件的函数无法在POST表单之后检索‘.csv’文件.请求上的文件元数据丢失,我找不到原因.更多详细信息如下:

我看到的错误:

Internal Server Error: /zip_app/
Traceback (most recent call last):
  File "/home/louis/.cache/pypoetry/virtualenvs/zip-radius-app-1xSGHScf-py3.10/lib/python3.10/site-packages/django/utils/datastructures.py", line 84, in __getitem__
    list_ = super().__getitem__(key)
KeyError: 'csvFile'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/louis/.cache/pypoetry/virtualenvs/zip-radius-app-1xSGHScf-py3.10/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/home/louis/.cache/pypoetry/virtualenvs/zip-radius-app-1xSGHScf-py3.10/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/louis/.cache/pypoetry/virtualenvs/zip-radius-app-1xSGHScf-py3.10/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 65, in _view_wrapper
    return view_func(request, *args, **kwargs)
  File "/home/louis/Documents/github/zip_radius_tool/zip_radius_app/zip_app/views.py", line 30, in index
    f = request.FILES['csvFile']
  File "/home/louis/.cache/pypoetry/virtualenvs/zip-radius-app-1xSGHScf-py3.10/lib/python3.10/site-packages/django/utils/datastructures.py", line 86, in __getitem__
    raise MultiValueDictKeyError(key)
django.utils.datastructures.MultiValueDictKeyError: 'csvFile'

用户提交.csv文件的索引页的HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<title>W3.CSS Template</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
body {font-family: "Lato", sans-serif}
.mySlides {display: none}
</style>
</head>
<body>

<!-- Page content -->
<div class="w3-content" style="max-width:2000px;margin-top:46px">

  
  <!-- The Band Section -->
  <div class="w3-container w3-content w3-center w3-padding-64" style="max-width:800px" id="band">       
        <form action="" method="post">
          <input type="file" name="csvFile" accept=".csv" enctype="multipart/form-data">
          <button type="submit" formmethod="post">Submit using POST</button>
        </form>
  </div>
  
<!-- End Page Content -->
</div>

</body>
</html>

和我的views.py文件(正在正确调用‘index’函数,但在‘f.quest.FILES[’csvFile‘]’上调用失败):

from django.shortcuts import render
from uszipcode import SearchEngine
import pandas as pd
from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
# Create your views here.
def index(request):
    context = {}
    if request.method == 'POST':
        d = request.POST
        print(d)
        f = request.FILES['csvFile']
        print(type(f))
        radius_results = pull_radius_zips(f)
        print('checking this works to this point')
    return render(request, "zip_app/index.html", context)

起初,我认为在提交表单时重定向到相同的视图可能有问题,但我发现这实际上是一件标准的事情.此外,当提交是POST时,索引视图可以正确捕获.

我还看到quest.POST正确地填充了我的测试文件的名称(‘Zip_codes.csv’)作为值,并且位于变量‘csvFile’下,因此CSV文件从请求对象中删除.

其他信息:我已为此视图禁用了CSRF令牌判断.

推荐答案

你忘了在表格里填enctype="multipart/form-data了.你把它放错了地方.

<form action="" method="post" enctype="multipart/form-data">
   ..........
</form>

这就是您的表单不处理请求中的文件的原因.这应该可以解决这个问题.

Html相关问答推荐

为什么在这种情况下是弹性基础:自动不解决内容的大小?

如何使用bslb设置可导航页面侧边栏的样式

在Quarto/Discrealjs演示文稿中只增加一个列表的填充

仅在加载视频时显示描述(<;Video>;标签)

使用CSS Flexbox时,我的图像未对齐

阴影部分内部的ionic Select 元素

在移动屏幕上显示时分支树视图的响应能力问题

:after 伪元素没有出现,即使它有 content 属性

如何正确地嵌套Flexbox

如何使用 CSS 应用带有笔划的文本阴影?

如何将 HTML DateTime 转换为 Golang Time 对象

使用图像自定义 CSS 网格边框

调整窗口大小时文本重叠导航栏

如何使用 CSS 和 HTML 将文本放入图像中?

如何使我的下拉菜单响应?

提交前的验证表单未显示任何验证消息?

从右到左支持百分号

CSS 网格自动高度不适用于特定网格区域

我怎么能有 :not(.class):nth-of-type(even)?

是否可以在换行时向锚标记添加填充?