Flask Response
Flask Response
response
服务端向客户端发送的响应
响应的几种方式
1.返回字符串(不常用)
return 'response OK!'
2.模板渲染 (前后端不分离的情况)
from flask import render_template
return render_template('index.html',name='张三',age=33)
3.返回json数据(一般前后端分离的情况)
data = {'name':'张三','age':44}
return data
或
data = {'name':'张三','age':44}
# jsonify 序列化,字典-->字符串
return jsonify(data) (推荐)
4.自定义response对象
(1)
html = render_template('index.html', name='张三', age=33)
print(html, type(html)) # <class 'str'> 说明render_template已经做了模板渲染,并且生成了html代码字符串
res = make_response(html, 200) # 200 状态码,可以改
return res
(2)
html = render_template('index.html', name='张三', age=33)
print(html, type(html)) # <class 'str'> 说明render_template已经做了模板渲染,并且生成了html代码字符串
res = Response(html)
return res
本文作者:春游去动物园
本文链接:https://www.cnblogs.com/chunyouqudongwuyuan/p/17463231.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2022-06-07 django URL问题(代码中url书写规范),django 多个APP时 static文件问题,多个app下的templates中的同名html文件查找顺序