我正在制作我的第一个Python/FlaskAPI。我目前正在处理post请求,该请求应该包含一个JSON对象,如下所示。根据用户输入,每个值都应该是整数或null
{
"FIDE": {
"standard": 1555,
"rapid": 1500,
"blitz": null
},
"USCF": {
"regular": null,
"quick": null,
"blitz": null
},
"Chesscom": {
"bullet": null,
"blitz": 1556,
"rapid": 1601,
"daily": null,
"puzzle": null
},
"LiChess": {
"bullet": null,
"blitz": null,
"rapid": null,
"classical": null,
"correspondence": null,
"training": null
}
}
然后输入应该点击这个基本的烧瓶应用程序。我应该注意,我刚刚创建了模型,我不确定是否正确处理了深度大于1的对象
from flask import Flask , request, redirect, url_for, Response
from flask_sqlalchemy import SQLAlchemy
import json
app = Flask(__name__)
db= SQLAlchemy(app)
class Player(db.model):
__tablename__ = 'Players'
id = db.Column(db.Integer, primary_key=True)
FIDE.standard(db.Integer, nullable = True)
FIDE.rapid(db.Integer, nullable = True)
FIDE.blitz(db.Integer, nullable = True)
USCF.regular(db.Integer, nullable = True)
USCF.quick(db.Integer, nullable = True)
USCF.blitz(db.Integer, nullable = True)
Chesscom.bullet(db.Integer, nullable = True)
Chesscom.blitz(db.Integer, nullable = True)
Chesscom.rapid(db.Integer, nullable = True)
Chesscom.daily(db.Integer, nullable = True)
Chesscom.puzzle(db.Integer, nullable = True)
Lichess.bullet(db.Integer, nullable = True)
Lichess.blitz(db.Integer, nullable = True)
Lichess.rapid(db.Integer, nullable = True)
Lichess.correspondence(db.Integer, nullable = True)
Lichess.training(db.Integer, nullable = True)
@app.route('/')
def index():
return 'Hello David'
@app.route('/add', methods = ['POST'])
def add(Player):
request_data= Player.getjson()
response = Response(request_data.FIDE.standard, status=200, mimetype='application/json')
return response
当我向邮递员发送请求时,我收到一条错误消息,上面写着
TypeError: exceptions must derive from BaseException
我相信,这是错误信息的相关部分,但我可能错了。我将在下面列出全部错误。但是,有人知道我在app.py文件中做错了什么吗
下面是完整的错误消息
Traceback (most recent call last):
File "C:\Users\dreke\Documents\coding\sideProject\chess-backend\venv\Lib\site-packages\flask\cli.py", line 354, in __call__
self._flush_bg_loading_exception()
File "C:\Users\dreke\Documents\coding\sideProject\chess-backend\venv\Lib\site-packages\flask\cli.py", line 342, in _flush_bg_loading_exception
raise exc_info
TypeError: exceptions must derive from BaseException
我认为这是一个烧瓶问题,正如您在以下PR中所看到的: https://github.com/pallets/flask/pull/4169
PR尚未合并,因此您必须使用https://github.com/Rohan-Salwan/flask/tree/dev或等待Flask版本与合并的PR一起发布
如果您将JSON作为原始数据作为POST请求在正文中传递,那么以下代码应该可以工作-
如果您是作为表单数据传递的,那么将
request_data = request.get_json()
替换为request_data= request.form
,它将完成这项工作为了使用SQLAlchemy操作,可能需要进行以下更改才能使其正常工作-
相关问题 更多 >
编程相关推荐