视图函数返回值

视图函数的返回值是有限制的,只能返回str, json,render_template,元组以及Response对象(或子类对象)

 

直接看例子:

from flask import Flask, jsonify, Response

app = Flask(__name__)


@app.route('/hello')
def hello_world():
    return 'hello_world'


@app.route('/add')
def add():
    return "this is add"


dict_1 = {"name": "tom", "age": 1}


@app.route('/add1')
def add1():
    return jsonify(dict_1)


@app.route('/add2')
def add2():
    return '123', 200

@app.route('/test')
def test():
    return render_template('index.html')

@app.route('/add3')
def add3():
    return Response("add3")

if __name__ == '__main__': app.run(host='127.0.0.1', port='8001')

 

实际上werkzeug要求每次请求的返回值是Response类型。所以实际上,flask的视图函数返回值无论如何变,它都不会离开Response类型

posted @ 2022-03-24 15:21  Target_L  阅读(69)  评论(0)    收藏  举报