url路由
请求方式限定
使用 methods 参数指定可接受的请求方式,可以是多种
@app.route('/',methods=['GET'])
def hello():
return '<h1>hello world</h1>'
路由查找方式
同一路由指向两个不同的函数,在匹配过程中,至上而下依次匹配
@app.route('/')
def hello():
return '<h1>hello world</h1>'
@app.route('/')
def hello_2017():
return '<h1>hello 2017</h1>'
所以上面路由 /
输出的结果为 hello 函数的结果
路由传参示例
有时我们需要将同一类URL映射到同一个视图函数处理,比如:使用同一个视图函数 来显示不同用户的个人信息。
从路由中提取url参数时需要使用flask中的路由转换器,所谓的路由转换器提取参数其实就是在进行url匹配过程中根据预先设定的 正则表达式来提取url地址中的某一部分作为参数,然后传递给对应的视图。
@app.route('/user/<int:id>')
def hello_itheima(id):
return 'hello itcast %d' %