字符串显示:
@app.route('/') def hello_world(): return 'Hello World!'
json显示:
不导入jsonify的处理:
@app.route("/json") def json(): import json data = { "a":"b" } response = make_response( json.dumps( data ) ) response.headers["Content-Type"] = "application/json" return response
一般使用导入jsonify的处理形式
from flash import json,make_response,jsonify @app.route("/json") def json(): user={"name":"zhou"} response=make_response(jsonify(user)) return response
页面渲染【在根级别目录建立文件夹templates/index.html】:
@app.route("/index") def index(): return render_template("index.html")
页面渲染间的数据传递实现问题:
1)传递一般变量
def index(): user="xiaoli" return render_template("index.html",name=user) html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p>this is json</p> <p> 用户:{{ name }} </p> </body> </html>
传递字典,在字典中基于字典或者列表传输数据:
def index(): context={} context['user'] = { "nickname":"编程浪子","qq":"998945","home_page":"http://www.54php.cn" } context['list'] = [1,2,3] return render_template("index.html",**context)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <p>this is json</p> <p> {% if user %} {{ user.qq }} {% endif %} </p> <p> {% for i in list %} {{ i }} {% endfor %} </p> </body> </html>
结果: