首页 > 其他分享 >Flask Response

Flask Response

时间:2023-06-07 14:48:11浏览次数:21  
标签:return name render Flask age html template 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

标签:return,name,render,Flask,age,html,template,Response
From: https://www.cnblogs.com/chunyouqudongwuyuan/p/17463231.html

相关文章

  • docker: Error response from daemon: could not select device driver "" with capab
    docker19之后的版本1.nanonvidia.shsudocurl-s-Lhttps://nvidia.github.io/nvidia-container-runtime/gpgkey|\ sudoapt-keyadd-distribution=$(./etc/os-release;echo$ID$VERSION_ID)sudocurl-s-Lhttps://nvidia.github.io/nvidia-container-runtime/$dist......
  • SpringMVC里通过ResponseBodyAdvice接口实现统一自定义返回逻辑
    这个org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice接口。publicinterfaceResponseBodyAdvice<T>{//返回true代表走自定义逻辑booleansupports(MethodParameterreturnType,Class<?extendsHttpMessageConverter<?>>converte......
  • Flask中url_for的使用传参,及多个参数
    flask.url_for(endpoint,**values)参数:endpoint-URL的端点(函数名称)values-URL规则的变量参数_external-如果设置为True,则生成绝对URL。服务器地址可以通过SERVER_NAME配置变量更改,默认为localhost。_scheme-指定所需URL方案的字符串。该_external参数必须设......
  • Flask之钩子函数
    Flask之钩子函数类似django的中间件,作用就是在进入框架的之后http方法之前或返回response之前进行一些操作Flask的钩子函数可在注册时根据注册的app或者蓝图从而确定钩子函数作用的范围(可全局也可作用某一个蓝图)钩子函数钩子函数可以分为两层说明,第一层是 app 层,第二层则......
  • response.setHeader()的用法
    response.setHeader()的用法response.setHeader()下载中文文件名乱码问题收藏 1.HTTP消息头(1)通用信息头即能用于请求消息中,也能用于响应信息中,但与被传输的实体内容没有关系的信息头,如Data,Pragma主要:Cache-Control,Connection,Data,Pragma,Trailer,Transfer-E......
  • Request类源码分析、序列化组件介绍、序列化类的基本使用、常用字段类和参数、反序列
    目录一、Request类源码分析二、序列化组件介绍三、序列化类的基本使用查询所有和查询单条四、常用字段类和参数(了解)常用字段类字段参数(校验数据来用的)五、反序列化之校验六、反序列化之保存七、APIVIew+序列化类+Response写的五个接口代码八、序列化高级用法之source(了解)九、......
  • Flask Model 做分页
    #手动做分页persons=Person.query.offset((page-1))*per_page).limit(per_page)#1.手动翻页#offset().limit()#数据:1,2,3,4,5,6,7,8,9,10#页码:page=1#每页显示数量:per_page=5#page=1:1,2,3,4,5=>offset(0).limit(5)#page=2:6,7,8,9,10=>offset(5).limi......
  • Flask 模型基础
    Flask模型Flask默认并没有任何数据库操作的API我们可以选择认可适合自己项目的数据库来使用Flask中可以根据自己的选择用原声语句实现功能,也可以选择ORM(SQIAlchemy、MongoEngine) ORMFlask用过Model操作数据库,不管你数据库的类型是Mysql或者Sqlite,Flask自动帮你生成相应数......
  • springMVC的controller中获取request,response对象的两个方法
    @GetMapping("outLogin")publicStringoutLogin(){ServletRequestAttributesattributes=(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();HttpServletRequestrequest=attributes.getRequest();......
  • HTTP/1.1 Request/Response
    https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.htmlhttps://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html RequestsHeader|HttpHeaderHeader解释示例Accept指定客户端能够接收的内容类型Accept:text/plain,text/htmlAccept-Charset浏览器可以接......