TypeError:无法散列的类型:上传文件时在Flask Form中设置了"set" [英] TypeError: unhashable type: 'set' in Flask Form while uploading file

查看:74
本文介绍了TypeError:无法散列的类型:上传文件时在Flask Form中设置了"set"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to fetch data from a form in Flask the form has a file as well:

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是上传的文件).

It is a simple form (not Flask-WTF form) and a file is being uploaded (here opencv.png is the file uploaded).

错误:

您尝试访问文件模型"在request.files字典中,但不存在.该请求的模仿类型是"application/x-www-form-urlencoded".而不是多部分/表单数据"这意味着没有文件内容被传输.要解决此错误,您应该提供enctype ="multipart/form-data"在您的表单中.

You tried to access the file "model" in the request.files dictionary but it does not exist. The mimetype for the request is "application/x-www-form-urlencoded" instead of "multipart/form-data" which means that no file contents were transmitted. To fix this error you should provide enctype="multipart/form-data" in your form.

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)

flask.debughelpers.FormDataRoutingRedirect:b'请求已发送到此URL( http://127.0.0.1:5000/newProjectCreation ),但是路由系统自动将重定向发布到"http://127.0.0.1:5000/newProjectCreation/".该URL定义了一个斜杠,因此如果没有访问Flask,它将自动重定向到带有斜杠的URL.请确保直接将您的POST请求发送到该URL,因为我们无法使浏览器或HTTP客户端可靠地重定向表单数据或在没有用户交互的情况下进行重定向.\ n \ n请注意:此异常仅在调试模式下引发

flask.debughelpers.FormDataRoutingRedirect: b'A request was sent to this URL (http://127.0.0.1:5000/newProjectCreation) but a redirect was issued automatically by the routing system to "http://127.0.0.1:5000/newProjectCreation/". The URL was defined with a trailing slash so Flask will automatically redirect to the URL with the trailing slash if it was accessed without one. Make sure to directly send your POST-request to this URL since we can't make browsers or HTTP clients redirect with form data reliably or without user interaction.\n\nNote: this exception is only raised in debug mode'

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)

flask.debughelpers.FormDataRoutingRedirect:b'请求已发送到此URL( http://127.0.0.1:5000/newProjectCreation ),但是路由系统自动将重定向发布到"http://127.0.0.1:5000/newProjectCreation/".该URL定义了一个斜杠,因此如果没有访问Flask,它将自动重定向到带有斜杠的URL.请确保直接将您的POST请求发送到该URL,因为我们无法使浏览器或HTTP客户端可靠地重定向表单数据或在没有用户交互的情况下进行重定向.\ n \ n请注意:此异常仅在调试模式下引发

flask.debughelpers.FormDataRoutingRedirect: b'A request was sent to this URL (http://127.0.0.1:5000/newProjectCreation) but a redirect was issued automatically by the routing system to "http://127.0.0.1:5000/newProjectCreation/". The URL was defined with a trailing slash so Flask will automatically redirect to the URL with the trailing slash if it was accessed without one. Make sure to directly send your POST-request to this URL since we can't make browsers or HTTP clients redirect with form data reliably or without user interaction.\n\nNote: this exception is only raised in debug mode'

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)

flask.debughelpers.DebugFilesKeyError:您尝试访问文件模型"在request.files字典中,但不存在.该请求的模仿类型是"application/x-www-form-urlencoded".而不是多部分/表单数据"这意味着没有文件内容被传输.要解决此错误,您应该提供enctype ="multipart/form-data"在您的表单中.

flask.debughelpers.DebugFilesKeyError: You tried to access the file "model" in the request.files dictionary but it does not exist. The mimetype for the request is "application/x-www-form-urlencoded" instead of "multipart/form-data" which means that no file contents were transmitted. To fix this error you should provide enctype="multipart/form-data" in your form.

相反,浏览器传输了一些文件名.该文件已提交:"opencv.png"

The browser instead transmitted some file names. This was submitted: "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方面的经验,正在学习,请尽可能简化回答.

先谢谢您.

推荐答案

在return语句中,您使用的是"{{}}",只需将其删除即可.应该是这样

In your return statement, you are using "{{}}", just remove those and it will work fine. It should be like this

return e

这篇关于TypeError:无法散列的类型:上传文件时在Flask Form中设置了"set"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆