Jsonify not defined - Internal server error

Playing with Flask and just wanted to print some data in JSON format, but I keep getting the error:

NameError: global name "jsonify" 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() 

Their documentation says that I either need to install Python 2.6 or simplejson - I have both.

Python 2.7.3:

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

simplejson:

root @Python: ~ / PythonScripts # pip install simplejson The requirement has already been met (use --upgrade to upgrade): simplejson in / usr / local / lib / python 2.7 / site-packages Cleaning up ...

+6
source share
1 answer

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

So, you will need to import it.
Change the start of the script to:

 from flask import jsonify # <- `jsonify` instead of `json` 
+23
source

Source: https://habr.com/ru/post/916495/


All Articles