route規則

規則

寫法: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'])

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章