7、flask-response响应

# 路由 + 视图函数

from flask import Blueprint, request, render_template, jsonify, Response

# from models import *

#蓝图
# 创建蓝图对象
# 第一个参数:蓝图的名字
# 第二个参数:蓝图的包名
blue = Blueprint('user', __name__,)

@blue.route('/') # 路由
def index():
    return 'user index'


# 请求和响应

#response响应
@blue.route('/response/')
def get_response():
    pass
    #响应的几种方式
    # 1. 返回字符串
    # return 'response'

    # 2.模板渲染(前后端不分离)
    # return render_template('index.html', name='zhangsan', age=18)

    # # 3.返回json数据(前后端分离)
    # data = {'name': 'zhangsan', 'age': 18}
    # # return data
    # return jsonify(data)    # 序列化、将字典转为字符串
    #
    # 4. 自定义响应对象
    html = render_template('index.html', name='zhangsan', age=18)
    print(html, type(html))
    # res = make_response(html, 200)
    res = Response(html, 200)

    return res

 

posted @ 2024-07-07 15:05  little小新  阅读(1)  评论(0编辑  收藏  举报