Skip to content

路由

常用写法

python
@app.route('/')
def index():
    return '<h1>Hello World!</h1>'

动态路由:尖括号里的内容就是动态部分

python
@app.route('/user/<name>')
def user(name):
    return '<h1>Hello, {}!</h1>'.format(name)

支持在路由中使用string、int、float和path类型。

path类型是一种特殊的字符串,与string类型不同的是,它可以包含正斜线。

python

@app.route('/user/<int:id>')
def user2(id):
    return '<h1>Hello, {}!</h1>'.format(id)

查询路由列表

python
print(app.url_map)

查看代码: https://gitee.com/PatrickW/flask-web/blob/master/src/2route.py

Map([<Rule '/static/<filename>' (HEAD, GET, OPTIONS) -> static>,
 <Rule '/' (HEAD, GET, OPTIONS) -> index>,
 <Rule '/user/<name>' (HEAD, GET, OPTIONS) -> user>,
 <Rule '/user/<id>' (HEAD, GET, OPTIONS) -> query_user>])