我正在尝试处理Django2.2中用户上传的文件,看起来GET请求正在工作,但是POST抛出了多值dictKeyError。你知道吗
我在YouTube上关注维托·弗雷塔斯的《光荣的Django File Upload Tutorial。你知道吗
大约5分钟后,讲师开始添加POST-request方法。我的项目在一些名称和我正在使用的其他应用程序方面略有不同。我正在本地开发环境中运行Django 2.2,不打算将来部署这个项目。你知道吗
每一行我都仔细检查了好几次。你知道吗
当我把“POST”换成“GET”时,MultiValueDictKeyError错误消失了,网页运行时没有出现错误,但是我的Django shell没有打印文件名和大小,正如我所期望的那样。你知道吗
你知道吗视图.py地址:
from django.shortcuts import render
from django.views.generic import TemplateView
class Home(TemplateView):
template_name = "home.html"
def upload(request):
if request.method == 'POST':
uploaded_file = request.FILES['document']
print(uploaded_file.name)
print(uploaded_file.size)
return render(request, 'upload.html')
我的上传.html地址:
{% block content %}
<h1> Eureka! </h1>
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="document">
<button type="submit">Upload file</button>
</form>
{% endblock %}
你知道吗网址.py地址:
from django.contrib import admin
from django.urls import path, re_path
# from . import views
from posts.views import *
from redactors.views import *
from counters.views import *
from AllAbove.views import *
from django.conf.urls.static import static
from django.conf import settings
from uploads.views import *
urlpatterns = [
path('admin/', admin.site.urls),
path('', home, name='home'),
path('result/', result, name='result'),
path('seth/', counters, name='seth'),
path('uploads/', upload, name='upload'),
#path('james/', post_details, name='james'),
path('maggie/', maggie_post_details, name='maggie'),
path('AllAbove/', all_above, name='AllAbove'),
re_path(r'^posts/(?P<post_id>[0-9]+)/$', post_details, name='james'),
path('simon/', redactors, name='simon'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL,
我希望模板会提示用户上传一个文件(比如图像),然后在Django shell中显示文件名和大小。相反,我收到的是多值dictkeyerror。此错误表示“文档”名称的某个位置有问题。据我所知,“文档”在模板和中都被正确引用视图.py你知道吗
以下是完整的错误和回溯:
Request Method: POST Request URL: http://127.0.0.1:8000/uploads/ Django Version: 2.2 Exception Type: MultiValueDictKeyError Exception Value:'document' Exception Location: /home//dev/projects/python/2018-and-2019/CC_Redact_Iter2/venv/lib/python3.7/site-packages/django/utils/datastructures.py in getitem, line 80 Python Executable: /home//dev/projects/python/2018-and-2019/CC_Redact_Iter2/venv/bin/python3 Python Version: 3.7.3 Python Path:
['/home//dev/projects/python/2018-and-2019/CC_Redact_Iter2', '/home//dev/projects/python/2018-and-2019/CC_Redact_Iter2/venv/lib/python37.zip', '/home//dev/projects/python/2018-and-2019/CC_Redact_Iter2/venv/lib/python3.7', '/home//dev/projects/python/2018-and-2019/CC_Redact_Iter2/venv/lib/python3.7/lib-dynload', '/usr/lib64/python3.7', '/usr/lib/python3.7', '/home//dev/projects/python/2018-and-2019/CC_Redact_Iter2/venv/lib/python3.7/site-packages'] Server time: Fri, 19 Jul 2019 16:56:46 +0000
以及:
$ python3 manage.py runserver Watching for file changes with StatReloader Performing system checks... System check identified no issues (0 silenced). July 19, 2019 - 16:57:53 Django version 2.2, using settings 'CC_Redact_Iter2.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C. Internal Server Error: /uploads/ Traceback (most recent call last): File "/home//dev/projects/python/2018-and-2019/CC_Redact_Iter2/venv/lib/python3.7/site-packages/django/utils/datastructures.py", line 78, in getitem list_ = super().getitem(key) KeyError: 'document' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home//dev/projects/python/2018-and-2019/CC_Redact_Iter2/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner response = get_response(request) File "/home//dev/projects/python/2018-and-2019/CC_Redact_Iter2/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "/home//dev/projects/python/2018-and-2019/CC_Redact_Iter2/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home//dev/projects/python/2018-and-2019/CC_Redact_Iter2/uploads/views.py", line 9, in upload uploaded_file = request.FILES['document'] File "/home//dev/projects/python/2018-and-2019/CC_Redact_Iter2/venv/lib/python3.7/site-packages/django/utils/datastructures.py", line 80, in getitem raise MultiValueDictKeyError(key) django.utils.datastructures.MultiValueDictKeyError: 'document' [19/Jul/2019 16:57:57] "POST /uploads/ HTTP/1.1" 500 79510
只需使用MultiValueDict的
get()
方法从request
字典获取数据。你知道吗相关问题 更多 >
编程相关推荐