TypeError: unhashable type: 'set' in Flask Form while uploading file
TypeError: unhashable type: 'set' in Flask Form while uploading file
我正在尝试从 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 词典中的文件“model”,但它不存在。请求的 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)
flask.debughelpers.FormDataRoutingRedirect: b'向此 URL (http://127.0.0.1:5000/newProjectCreation) 发送了一个请求,但路由系统自动发出重定向到“http://127.0.0.1 :5000/新建项目/”。 URL 是用尾部斜杠定义的,因此如果在没有斜杠的情况下访问它,Flask 将自动重定向到带有尾部斜杠的 URL。请确保将您的 POST-请求直接发送到此 URL,因为我们无法使浏览器或 HTTP 客户端可靠地或在没有用户交互的情况下使用表单数据进行重定向。\n\nNote:此异常仅在调试模式下引发'
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/新建项目/”。 URL 是用尾部斜杠定义的,因此如果在没有斜杠的情况下访问它,Flask 将自动重定向到带有尾部斜杠的 URL。请确保将您的 POST-请求直接发送到此 URL,因为我们无法使浏览器或 HTTP 客户端可靠地或在没有用户交互的情况下使用表单数据进行重定向。\n\nNote:此异常仅在调试模式下引发'
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 词典中的文件“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经验,正在学习中,如果可以的话请简化回复。
提前谢谢你。
在您的 return 语句中,您使用的是“{{}}”,只需删除它们即可正常工作。
应该是这样
return e
我正在尝试从 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 词典中的文件“model”,但它不存在。请求的 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)
flask.debughelpers.FormDataRoutingRedirect: b'向此 URL (http://127.0.0.1:5000/newProjectCreation) 发送了一个请求,但路由系统自动发出重定向到“http://127.0.0.1 :5000/新建项目/”。 URL 是用尾部斜杠定义的,因此如果在没有斜杠的情况下访问它,Flask 将自动重定向到带有尾部斜杠的 URL。请确保将您的 POST-请求直接发送到此 URL,因为我们无法使浏览器或 HTTP 客户端可靠地或在没有用户交互的情况下使用表单数据进行重定向。\n\nNote:此异常仅在调试模式下引发'
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/新建项目/”。 URL 是用尾部斜杠定义的,因此如果在没有斜杠的情况下访问它,Flask 将自动重定向到带有尾部斜杠的 URL。请确保将您的 POST-请求直接发送到此 URL,因为我们无法使浏览器或 HTTP 客户端可靠地或在没有用户交互的情况下使用表单数据进行重定向。\n\nNote:此异常仅在调试模式下引发'
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 词典中的文件“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经验,正在学习中,如果可以的话请简化回复。
提前谢谢你。
在您的 return 语句中,您使用的是“{{}}”,只需删除它们即可正常工作。 应该是这样
return e