Flask(2)-动态路由&转换器

发布时间 2023-11-28 17:43:18作者: doro测试笔记

动态路由

1 @app.route("/user/<name>")
2 def show_user(name):
3     return "My name is %s" % name

这里定义了动态路径:/user/<name>。

函数有一个参数:name。

 

转换器

flask中,参数类型默认是string,也可以指定其类型

1 @app.route("/age/<int:age>")
2 def show_age(age):
3     return 'age is %d' % age
4 
5 
6 @app.route('/path/<path:name>')
7 def show_path(name):
8     return 'path is %s' % name