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
本文作者:little小新
本文链接:https://www.cnblogs.com/littlecc/p/18288526
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步