python flask中动态URL规则

来源:互联网 发布:记账软件电脑版 编辑:程序博客网 时间:2024/06/05 10:57

URL是可以添加变量部分的,
把类似的部分抽象出来,
比如:

@app.route('/example/1/')@app.route('/example/2/')@app.route('/example/3/')def example(id):    return 'example:{ }'.format(id)

可以抽象为:

@app.route('/example/<id>/')def wxample(id):    return 'example:{ }'.format(id)

尖括号中的内容是动态的,id作为参数获得,
此时默认id为字符串类型
我们可以指定参数类型,
比如:

string:指定任何没有斜杠‘/’的文本(默认)
int:接受整数
float:同int,但是接受浮点数
path:和默认的很相似,但是可以接受斜杠
uuid:只接受uuid字符串
any:可以指定多种路径,但是需要传入参数

比如:
@app.route(‘/any(a,b)’:content_name/)
访问/a/和访问/b/都符合这个规则,/a/对应的content_name就是a.

原创粉丝点击