我正在尝试从 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/newProjectCreation/” . URL 是用尾部斜杠定义的,所以如果没有斜杠访问,Flask 将自动重定向到带有尾部斜杠的 URL。请确保将您的 POST 请求直接发送到此 URL,因为我们无法使浏览器或 HTTP 客户端在没有用户交互的情况下可靠地重定向表单数据。\n\n注意:此异常仅在调试模式下引发'
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注意:此异常仅在调试模式下引发'
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 方面的经验,正在学习中,如果可能,请简化回复。
提前致谢。
答案 0 :(得分:1)
在您的 return 语句中,您使用的是“{{}}”,只需删除它们即可。 应该是这样的
return e