我正在构建一个非常简单的应用程序,其中包括一个页面,用户在其中提交邮政编码列表,然后返回一个邮政编码位于原始列表的特定半径内的文件。
我的问题是,我处理views.py 文件中包含的文件的函数无法在发布表单后检索“.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.request.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 时,索引视图可以正确捕获。
我还看到 request.POST 正确填充了我的测试文件的名称(“zip_codes.csv”)作为值并位于变量“csvFile”下,因此 csv 文件正在从请求对象中删除。
其他信息:我为此视图禁用了 csrf 令牌检查。
您忘记在表单中使用
enctype="multipart/form-data
。您在输入中将其放置错误。
<form action="" method="post" enctype="multipart/form-data">
..........
</form>