繁体   English   中英

Flask / Python API:TypeError:异常必须从 BaseException 派生

[英]Flask / Python API : TypeError: exceptions must derive from BaseException

我正在制作我的第一个 Python/Flask API。 我目前正在处理我的 post 请求,它应该接收一个如下所示的 JSON 对象。 每个值都应该是基于用户输入的整数或空值。

{
  "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

当我向 Postman 发送请求时,我收到一条错误消息,说

类型错误:异常必须从 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

如果您将 JSON 作为正文中的原始数据作为 POST 请求传递,那么以下代码应该可以工作 -

from flask import Flask, request, redirect, url_for, Response
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)


@app.route('/')
def index():
    return 'Hello David'

@app.route('/add', methods = ['POST'])
def add():
    request_data = request.get_json()
    # You can make any change or operation using the request_data
    return request_data

if __name__ == '__main__':
    app.run(debug=True)

如果您传递的表单数据然后更换request_data = request.get_json()request_data= request.form和应做的工作。

为了使用 SQLAlchemy 操作,可能需要进行以下更改才能使其工作 -

from flask import Flask, request, redirect, url_for, Response
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

### Assuming you are using MySQL DB
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://username:password@localhost/db_name'

db= SQLAlchemy(app)


class Player(db.Model):

    __tablename__ = 'Players'

    id                     = db.Column(db.Integer, primary_key=True, autoincrement=True)
    FIDE_standard          = db.Column(db.Integer, nullable = True)
    FIDE_rapid             = db.Column(db.Integer, nullable = True)
    FIDE_blitz             = db.Column(db.Integer, nullable = True)
    USCF_regular           = db.Column(db.Integer, nullable = True)
    USCF_quick             = db.Column(db.Integer, nullable = True)
    USCF_blitz             = db.Column(db.Integer, nullable = True)
    Chesscom_bullet        = db.Column(db.Integer, nullable = True)
    Chesscom_blitz         = db.Column(db.Integer, nullable = True)
    Chesscom_rapid         = db.Column(db.Integer, nullable = True)
    Chesscom_daily         = db.Column(db.Integer, nullable = True)
    Chesscom_puzzle        = db.Column(db.Integer, nullable = True)
    Lichess_bullet         = db.Column(db.Integer, nullable = True)
    Lichess_blitz          = db.Column(db.Integer, nullable = True)
    Lichess_rapid          = db.Column(db.Integer, nullable = True)
    Lichess_correspondence = db.Column(db.Integer, nullable = True)
    Lichess_training       = db.Column(db.Integer, nullable = True)


@app.route('/')
def index():
    return 'Hello David'

@app.route('/add', methods = ['POST'])
def add():

    request_data= request.get_json()
    
    new_player = Player(
        FIDE_standard          = request_data.get('FIDE').get('standard'),
        FIDE_rapid             = request_data.get('FIDE').get('rapid'),
        FIDE_blitz             = request_data.get('FIDE').get('blitz'),
        USCF_regular           = request_data.get('USCF').get('regular'),
        USCF_quick             = request_data.get('USCF').get('quick'),
        USCF_blitz             = request_data.get('USCF').get('blitz'),
        Chesscom_bullet        = request_data.get('Chesscom').get('bullet'),
        Chesscom_blitz         = request_data.get('Chesscom').get('blitz'),
        Chesscom_rapid         = request_data.get('Chesscom').get('rapid'),
        Chesscom_daily         = request_data.get('Chesscom').get('daily'),
        Chesscom_puzzle        = request_data.get('Chesscom').get('puzzle'),
        Lichess_bullet         = request_data.get('Lichess').get('bullet'),
        Lichess_blitz          = request_data.get('Lichess').get('blitz'),
        Lichess_rapid          = request_data.get('Lichess').get('rapid'),
        Lichess_correspondence = request_data.get('Lichess').get('correspondence'),
        Lichess_training       = request_data.get('Lichess').get('training'),
    )

    db.session.add(new_player)
    db.session.commit()

    return request_data

if __name__ == '__main__':
    app.run(debug=True)

我相信这是一个 Flask 问题,您可以在以下 PR: https : //github.com/pallets/flask/pull/4169 中看到。

PR 尚未合并,因此您必须使用https://github.com/Rohan-Salwan/flask/tree/dev或等到 Flask 版本与合并的 PR 一起发布。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM