当前位置: 首页 > 知识库问答 >
问题:

TypeError:上载文件时无法损坏烧瓶表单中的类型:“set”

孙经艺
2023-03-14

我正在尝试从Flask中的表单获取数据,该表单也有一个文件:

app.py

@app.route('/newProjectCreation/', methods=['GET', 'POST'])
def newProjectCreation():
    try:
        if request.method == 'POST':
            # Data Fetch from Models Form
            name = request.form['modelName']
            modelDescription = request.form['modelDescription']
            inputType = request.form.getlist['inputType[]']
            outputType = request.form.getlist['outputType[]']
            model = request.files['model']      # file
            modelLanguage = request.form['modelLanguage']

HTML

这是一个简单的表单(不是Flask WTF表单),正在上载一个文件(此处opencv.png是上载的文件)。

错误:

您试图访问request.files字典中的文件“模型”,但它不存在。请求的mimetype是"Application/x-www-form-urlencoded"而不是"Multipart/form-data",这意味着没有传输文件内容。要修复此错误,您应该在表单中提供enctype="Multipart/form-data"。

The browser instead transmitted some file names. This was submitted: "opencv.png"
127.0.0.1 - - [11/May/2021 05:24:05] "POST /newProjectCreation HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1926, in dispatch_request
    self.raise_routing_exception(req)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1912, in raise_routing_exception
    raise FormDataRoutingRedirect(request)

瓶子调试助手。FormDataRoutingRedirect:b'A请求已发送到此URL(http://127.0.0.1:5000/newProjectCreation)但路由系统自动发出重定向到“http://127.0.0.1:5000/newProjectCreation/“。URL是用尾随斜杠定义的,因此如果在没有尾随斜杠的情况下访问,Flask将自动重定向到带有尾随斜杠的URL。请确保直接将您的POST请求发送到此URL,因为我们无法使浏览器或HTTP客户端使用表单数据可靠地重定向,或者在没有用户交互的情况下重定向。\n\n注意:此异常仅适用于rais。”“在调试模式下调试”

127.0.0.1 - - [11/May/2021 05:24:05] "POST /newProjectCreation HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1926, in dispatch_request
    self.raise_routing_exception(req)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1912, in raise_routing_exception
    raise FormDataRoutingRedirect(request)

瓶子调试助手。FormDataRoutingRedirect:b'A请求已发送到此URL(http://127.0.0.1:5000/newProjectCreation)但路由系统自动发出重定向到“http://127.0.0.1:5000/newProjectCreation/“。URL是用尾随斜杠定义的,因此如果在没有尾随斜杠的情况下访问,Flask将自动重定向到带有尾随斜杠的URL。请确保直接将您的POST请求发送到此URL,因为我们无法使浏览器或HTTP客户端使用表单数据可靠地重定向,或者在没有用户交互的情况下重定向。\n\n注意:此异常仅适用于rais。”“在调试模式下调试”

127.0.0.1 - - [11/May/2021 05:24:05] "POST /newProjectCreation/ HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/debughelpers.py", line 96, in __getitem__
    return oldcls.__getitem__(self, key)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/werkzeug/datastructures.py", line 442, in __getitem__
    raise exceptions.BadRequestKeyError(key)
werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/adiagarwal/fantastic-computing-machine/flaskapp-docker/flaskapp/app.py", line 108, in newProjectCreation
    model = request.files['model']
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/debughelpers.py", line 100, in __getitem__
    raise DebugFilesKeyError(request, key)

瓶子调试助手。DebugFilesKeyError:您试图访问请求中的文件“model”。文件字典,但它不存在。请求的mimetype是“application/x-www-form-urlencoded”,而不是“multipart/form data”,这意味着没有传输任何文件内容。要修复此错误,应在表单中提供enctype=“multipart/form data”。

浏览器反而传输了一些文件名。这是提交的:“opencv.png”

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/adiagarwal/fantastic-computing-machine/env/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/adiagarwal/fantastic-computing-machine/flaskapp-docker/flaskapp/app.py", line 129, in newProjectCreation
    return {{e}}
TypeError: unhashable type: 'set'

我没有js的经验,我正在学习,如果可能的话,请简化响应。

提前谢谢你。

共有1个答案

郭远
2023-03-14

在return语句中,您使用的是“{}”,只需删除它们,它就可以正常工作。应该是这样的

return e
 类似资料:
  • 问题内容: 我正在尝试使用Flask创建一个Web应用程序,该应用程序允许用户上传文件并将其提供给其他用户。现在,我可以将文件正确 地上 传到 upload_folder了 。但是我似乎找不到找到让用户下载回来的方法。 我将文件名的名称存储到数据库中。 我有一个为数据库对象服务的视图。我也可以删除它们。 在我的HTML中,我有: 和下载视图: 但它正在返回: 未找到 在服务器上找不到请求的URL。

  • 我基本上使用了install命令“$pip install Flask”,当我试图运行一个程序时,它会说“找不到模块”Flask安装在“/usr/local/lib/python2.7/site包”中,但我认为pip的意义在于,我可以到处导入这些包。我试图在我的桌面上运行一个文件,甚至当我将Flask文件夹移动到桌面上时,它也不起作用。有什么建议吗?谢谢

  • 我发现很难找到有关这方面的资料。会是什么?我如何解决这个问题?有哪些可能的修复方法? UWSGI日志文件 时钟来源:unix检测到CPU核数:4当前工作目录:/home/pi检测到二进制路径:/usr/local/bin/uwsgi!!!没有内部路由支持,重建与pcre支持!!!*警告:您在没有主进程管理器的情况下运行uWSGI进程数限制为7336内存页大小为4096字节检测到最大文件描述符号:6

  • 我编写的下载文件的方法总是产生损坏的文件。 我通过adb访问这些文件,将它们传输到我的sccard,在那里我看到它们似乎有合适的大小,但没有根据例如Linux命令的类型。 你知道丢失了什么以及如何修复它吗? 谢谢。 代码的简单版本(但错误相同) 日志:< code > file . length:2485394 | content length:1399242 问题是,我从我的API单例中获得了,

  • 问题内容: 我使用Eclipse在Windows 7中创建了一个jar文件。当我尝试打开jar文件时,它说jar文件无效或损坏。谁能建议我为什么jar文件无效? 问题答案: 当您在Windows资源管理器中双击一个JAR文件时,会发生这种情况,但是JAR本身实际上不是 可执行的 JAR。真正的可执行JAR至少应具有带有方法的类,并在中引用它。 在Eclispe中,您需要将项目导出为 Runnabl