首页 > 开发 > Python > 正文

python flask中动态URL规则详解

2020-07-28 14:02:45
字体:
来源:转载
供稿:网友

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.

以上这篇python flask中动态URL规则详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持错新网之家。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表