URL与函数的映射
格式:<converter:variable>
尖括号是固定写法,converter为URL转换器,variable为变量值,默认类型是字符串类型。
变量规则
序号 | URL转换器 | 描述 |
---|---|---|
1 | string | 默认类型字符串 |
2 | int | 接受整数 |
3 | float | 接受浮点型 |
4 | path | 接受用作目录分隔符的斜杠 |
5 | uuid | 接受uuid类型的字符串 |
6 | any | 可以指定多个值 |
使用示例
-
int
@app.route('/article/<int:p_id>') def one_process(p_id): return "article page %d" % p_id
-
string
@app.route('/article/content/<c>') def two_process(c): return "article details: %s" % c
-
float
@app.route('/article/float/<float:f>') def three_process(f): return "article float: %f" % f
-
path
@app.route('/article/path/<path:p>') def four_process(p): return "article path: %s" % p
-
any
@app.route('/article/any/<any(blog, email):a>') def five_process(a): if a == "blog": return "article any: 博客%s" % a if a == 'email': return "article any: 邮箱%s" % a
-
uuid
先生成uuid再测试import uuid print(uuid.uuid4()) # uuid:bce756d8-535a-425a-9e00-cfb524d53521
@app.route('/article/getuuid/<uuid:u>') def six_process(u): print(u) return "article uuid: %s" % u