gpt4 book ai didi

python - 未定义 jsonify - 内部服务器错误

转载 作者:太空狗 更新时间:2023-10-29 17:36:00 25 4
gpt4 key购买 nike

玩弄 Flask,只想打印出一些 JSON 格式的数据,但我不断收到错误消息:

NameError: global name 'jsonify' is not defined

from flask import Flask
from flask import json
app = Flask(__name__)

@app.route("/")
def testJSON():
x = "Test1"
y = "Test2"
return jsonify(a=x,z=y)

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

他们的 documentation说我需要安装 Python 2.6 或 simplejson - 我都有。

python 2.7.3:

sys.version '2.7.3 (default, May 9 2012, 23:42:16) \n[GCC 4.4.3]'

简单的json:

root@Python:~/PythonScripts# pip install simplejson Requirement already satisfied (use --upgrade to upgrade): simplejson in /usr/local/lib/python2.7/site-packages Cleaning up...

最佳答案

jsonify() is a function contained within the flask module .

所以你需要导入它。
将脚本的开头更改为:

from flask import jsonify # <- `jsonify` instead of `json`

关于python - 未定义 jsonify - 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10727366/

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