规则
写法:converter:variable_name
converter类型:
string 字符串
int 整形
float 浮点型
path 接收路径,接收的时候是str,/也当做字符串的一个字符
uuid 只接受uuid字符串
any 可以同时指定多种路径,进行限定
例子:
@app.route('/')
@app.route('/get_id/<id>/')
@app.route('/get_float_id/<float:uid>/')
@app.route('/get_path/<path:upath>/')
实现对应的视图函数:
@blue.route('/')
def hello_world():
return 'Hello Word!'
# 路由匹配规则
# 1、<id>: 默认接收的类型是str
# 2、<string:id>: 指定接收的类型是str(字符串)
# 3、<int:id>: 指定接收的类型是int(整形)
# 4、<float:uid>: 指定接收的类型是float(浮点型)
# 4、<path:upath>: 指定接收的类型是path(URl中的路径)
@blue.route('/get_id/<id>/')
def get_id(id):
# 匹配str类型的id值
return 'id: %s' % id
@blue.route('/get_int_id/<int:id>/')
def get_int_id(id):
# 匹配int类型的id值
return 'id: %d' % id
@blue.route('/get_float_id/<float:uid>/')
def get_float_id(uid):
# 匹配float类型的uid值
return 'id: %.3f' % uid
@blue.route('/get_path/<path:upath>/')
def get_path(upath):
# 匹配path类型的upath值
return 'path: %s' % upath
methods请求方法
常用的请求类型有如下几种
GET : 获取
POST : 创建
PUT : 修改(全部属性都修改)
DELETE : 删除
PATCH : 修改(修改部分属性)
定义url的请求类型:
@blue.route('/getrequest/', methods=['GET', 'POST'])
网友评论