Flask 中的 Response

1.Flask中的HTTPResponse

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

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

2.Flask中的Redirect

Flask 中的 Response

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

3.Flask 中的 render (render_template)

Flask 中的 Response

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

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

Flask 中的 Response

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

Flask 中的 Response

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

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

send_file("文件的路径")

5.标准JSON格式的Response

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

上一篇:$(document).ready(function(){})与window.load


下一篇:c/c++赋值函数(重载=号运算符)