''' 传递参数的语法是:`/<参数名>/`。然后在视图函数中,也要定义同名的参数。 ### 参数的数据类型: 1. 如果没有指定具体的数据类型,那么默认就是使用`string`数据类型。 2. `int`数据类型只能传递`int`类型。 3. `float`数据类型只能传递`float`类型。 4. `path`数据类型和`string`有点类似,都是可以接收任意的字符串,但是`path`可以接收路径,也就是说可以包含斜杠。 5. `uuid`数据类型只能接收符合`uuid`的字符串。`uuid`是一个全宇宙都唯一的字符串,一般可以用来作为表的主键。 6. `any`数据类型可以在一个`url`中指定多个路径。例如: ```python @app.route('/<any(blog,article):url_path>/<id>/') def detail(url_path,id): if url_path == 'blog': return '博客详情:%s' % id else: return '文章详情:%s' % id ``` ''' @app.route('/') def hello_world(): return 'Hello World!' @app.route('/list/') def article_list(): return 'article list' @app.route('/p/<float:article_id>') def article_detail(article_id): return '您请求的文章是:%s' % article_id # `path`数据类型和`string`有点类似,都是可以接收任意的字符串,但是`path`可以接收路径,也就是说可以包含斜杠。 @app.route('/article/<path:test>/') def test_article(test): return 'test article:%s' % test @app.route('/u/<uuid:user_id>/') def user_detail(user_id): return '用户个人中心页面:%s' % user_id # `any`数据类型可以在一个`url`中指定多个路径。例如: @app.route('/<any(blog,article):url_path>/<id>/') def detail(url_path,id): if url_path == 'blog': return '博客详情:%s' % id else: return '文章详情:%s' % id # 通过问号的形式传递参数 @app.route('/d/') def d(): wd = request.args.get('wd') ie = request.args.get('ie') print('ie:',ie) return '您通过查询字符串的方式传递的参数是:%s' % wd ''' ### 接收用户传递的参数: 1. 第一种:使用path的形式(将参数嵌入到路径中),就是上面讲的。 2. 第二种:使用查询字符串的方式,就是通过`?key=value`的形式传递的。 ```python @app.route('/d/') def d(): wd = request.args.get('wd') return '您通过查询字符串的方式传递的参数是:%s' % wd
flask url与视图函数的映射
最新推荐文章于 2023-03-03 16:35:28 发布