注册路由的多种方式

Flask 路由注册

  • app.routeapp.add_url_rule
  • 蓝图

app.route 和 app.add_url_rule

app.route

# -*- coding: utf-8 -*-
from flask import Flask
app = Flask( __name__ )

@app.route( "/" )
def hello():
    return "Hello ,I Love Imooc"

@app.route( "/my" )
def my():
    return "my page"

@app.route( "/my/<user_name>" )
def my( user_name ):
    return "my page:user %s" %( user_name )
    
if __name__ == "__main__":
    app.run( host = "0.0.0.0",debug=True )

app.add_url_rule

# -*- coding: utf-8 -*-
from flask import Flask
app = Flask( __name__ )

#@app.route( "/" )
def hello():
    return "Hello ,I Love Imooc"

#@app.route( "/my/<user_name>" )
def my( user_name ):
    return "my page:user %s" %( user_name )


app.add_url_rule( rule = "/",view_func = hello )
app.add_url_rule( rule = "/my/<user_name>",view_func = my )

if __name__ == "__main__":
    app.run( host = "0.0.0.0",debug=True )

蓝图

# -*- coding: utf-8 -*-
from flask import Flask,Blueprint
app = Flask( __name__ )

#蓝本的名字和蓝本所在的包或模块
index_page = Blueprint( "index_page",__name__ )
@index_page.route( "/" )
def index_page_index():
    return "index page"

app.register_blueprint( index_page,url_prefix = "/imooc" )

@app.route( "/" )
def hello():
    return "Hello ,I Love Imooc"


if __name__ == "__main__":
    app.run( host = "0.0.0.0",debug=True )

application.py负责蓝图注册

# -*- coding: utf-8 -*-
from flask import Flask
from indexController import index_page
from postController import post_page

app = Flask( __name__ )

app.register_blueprint( index_page,url_prefix = "/imooc" )
app.register_blueprint( post_page,url_prefix = "/post" )

if __name__ == "__main__":
    app.run( host = "0.0.0.0",debug=True )

indexController.py的写法

# -*- coding: utf-8 -*-
from flask import Flask,Blueprint
'''
post/index 列表
post/info 详情
post/set 添加|编辑
post/ops 操作(删除|恢复)
'''

index_page = Blueprint( "index_page",__name__ )

@index_page.route( "/" )
def index_page_index():
    return "index page"


@index_page.route( "/me" )
def hello():
    return "hello ,I Love Imooc"

一个python的文件有两种使用的方法,第一是直接作为脚本执行,第二是import到其他的python脚本中被调用(模块重用)执行。因此if __name__ == 'main': 的作用就是控制这两种情况执行代码的过程,在if __name__ == 'main': 下的代码只有在第一种情况下(即文件作为脚本直接执行)才会被执行,而import到其他脚本中是不会被执行的。

if __name__ == "__main__" #“__main__”等于当前执行文件的名称:

解读HTTP请求流程

HTTP请求流程

获取请求对象的GET和POST参数

POST工具

  • Linux:curl和Postman
  • Windows:Postman

get请求

from flask import Blueprint,request

get_page = Blueprint( "get_page",__name__ )

@get_page.route("/get")
def get():
    req = request.values
    var_a = req['a'] if "a" in req else "i love kevin"
    return "request:%s,params:%s,var_a:%s"%(request.method,request.args,var_a )

post请求

from flask import Blueprint,request

post_page = Blueprint( "post_page",__name__ )

@post_page.route("/post",methods = [ "POST" ])
def post():
    req = request.values
    var_a = req['a'] if "a" in req else "i love kevin"
    return "request:%s,params:%s,var_a:%s" % (request.method, request.form, var_a)

`post`请求

POST上传文件注意事项enctype="multipart/form-data"

@post_page.route("/upload",methods = [ "POST" ])
def upload():
    f = request.files['file'] if "file" in request.files else None
    return "request:%s,params:%s,file:%s"%(request.method,request.files,f )

POST上传文件注意事项