当前位置:主页 > 软件编程 > Python代码 >

python flask中动态URL规则详解

时间:2023-03-20 10:28:07 | 栏目:Python代码 | 点击:

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.

您可能感兴趣的文章:

相关文章