gpt4 book ai didi

flask - View 函数未返回有效响应。 flask 棉花糖,RestAPI

转载 作者:行者123 更新时间:2023-12-04 13:40:08 30 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Return JSON response from Flask view

(14 个回答)


去年关闭。




我在 Flask 上写申请.对于 RestAPI我在用 flask-marshmallow .但我得到错误。

The view function did not return a valid response. The return type must be a string, dict, tuple, Response instance, or WSGI callable, but it was a list. I do everything as in the example 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)

最佳答案

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

from flask import jsonify

@bp.route("")
def applications():
all_applications = Application.query.all()
return jsonify(applications_schema.dump(all_applications))

关于flask - View 函数未返回有效响应。 flask 棉花糖,RestAPI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58326703/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com