flask的路由系统

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
flask中最经典的设置路由的方式是通过装饰器
@app.route('/')

#route的参数
1.rule 路径 #这个必须要填
2.options
methods #允许进入的请求方式,例如GET,POST
endpoint #别名,用于做反向解析,不写,默认是函数名
default #给函数传递的参数
redirect_to #重定向的地址
strict_slashes = None #对URL最后的 / 符号是否严格要求

#转换器
我们还可以使用在路由中使用转换器
eg:
@app.route('/detail/<int:nid>',methods=['GET'],endpoint='detail')

flask我们还可以设置另外一种路由方式
#FBV式的
app.add_url_rule('/',endpoint='index',view_func=index)

#CBV式的
app.add_url_rule('/',endpoint='index',view_func=index.as_view(name='index')) #as_view中的name必填