Flask 中的 Response

1.Flask中的HTTPResponse

@app.route("/")  # app中的路由route装饰器
def index():  # 视图函数
    return "I am ZWQ"

在Flask 中的HttpResponse 在我们看来其实就是直接返回字符串

 

2.Flask中的Redirect

每当访问"/redi"这个地址的时候,视图函数redi会触发redirect("/") 跳转到url地址:  "/" 并会触发"/"对应的视图函数index()

 

3.Flask 中的 render (render_template)

HTML模板渲染是每个Web框架中都必须有的,至于render_template的具体用法,留个悬念,往后看

注意: 如果要使用 render_template 返回渲染的模板,请在项目的主目录中加入一个目录 templates

否则可能会有一个Jinja2的异常哦

遇到上述的问题,基本上就是你的template的路径问题

 

 4.打开并返回文件内容,并自动识别文件类型Content-Type:文件类型

send_file("文件的路径")

 

5.标准JSON格式的Response

  jsonify({name:1}) # Content-type:application/json

 

posted @ 2019-02-26 15:54  清风_Z  阅读(515)  评论(0编辑  收藏  举报