视图函数未返回有效响应。Flask-棉花糖,RestAPI

5 flask flask-restful

我在 上写申请Flask。为RestAPI我所用flask-marshmallow。但我收到错误。

视图函数未返回有效响应。返回类型必须是字符串、字典、元组、Response 实例或可调用的 WSGI,但它是一个列表。我按照示例中的方式进行所有操作 https://flask-marshmallow.readthedocs.io/en/latest/

class ApplicationSchema(ma.Schema):
    class Meta:
        fields = ("id", "name")

applications_schema = ApplicationSchema(many=True)

@bp.route("")
def applications():
    all_applications = Application.query.all()
    return applications_schema.dump(all_applications)
Run Code Online (Sandbox Code Playgroud)

小智 2

我可以通过将 return 放在 jsonify 中来纠正这个问题。您可以从 Flask 导入它,它看起来像这样:

from flask import jsonify

@bp.route("")
    def applications():
        all_applications = Application.query.all()
        return jsonify(applications_schema.dump(all_applications))
Run Code Online (Sandbox Code Playgroud)