flask 路由系統 引數

2022-03-05 13:23:10 字數 2592 閱讀 5879

methods : 當前 url 位址,允許訪問的請求方式      

當前檢視函式支援的請求方法     405    請求方式不被允許

/info

", methods=["

get", "

post"])

def info():

return

'go !!!

'endpoint : 反向url位址,預設為檢視函式名 (url_for)

/info

", methods=["

get", "

post"])

# def info():

# print(url_for(

"info"))

#

return

'go !!!'"

/info

", methods=["

get", "

post

"],endpoint='

r_info')

def info():

print(url_for(

"r_info

")) #/info

return

'go !!!

'defaults : 檢視函式的引數預設值

一旦預設引數存在 檢視函式中必須有乙個形參去接收 形參變數名必須與 defaults 中的一致

/info

", methods=["

get", "

post

"], endpoint='

r_info

', defaults=)

def info(nid):

print(url_for(

"r_info

")) # /info

print(nid)

return

'go !!!

'strict_slashes : url位址結尾符"/"的控制

false : 無論結尾 "/" 是否存在均可以訪問 ,

true : 結尾必須不能是 "/"

# 訪問位址 : /info 

"/info

", strict_slashes=true)

def student_info():

return

"hello""

/infos

", strict_slashes=false)

def student_infos():

return

"hello

"

redirect_to : url位址重定向

# 訪問位址 : /info 瀏覽器跳轉至 /infos

"/info

", redirect_to="

/infos")

def student_info():

return

"hello old boy info""

/infos")

def student_infos():

return

"hello old boy infos

"

subdomain : 子網域名稱字首 subdomian="dragonfire"

server_name

"] = "

oldboy.com""

/info

",subdomain="

dragonfire")

def student_info():

return

"hello old boy info

"# 訪問位址為: dragonfire.oldboy.com/info

就是在url後定義乙個引數接收

但是這種動態引數路由,在url_for的時候,一定要將動態引數名+引數值新增進去,否則會丟擲引數錯誤的異常

/info/

", methods=["

get", "

post

"]) #預設為字串型別

"/info/

", methods=["

get", "

post"])

"/info/

", methods=["

get", "

post"])

def info(nid): #注意接收引數

# print(url_for(

"r_info

")) # /info

print(nid,type(nid))

return

'go !!!

'

/detail//檔案

/info//

", methods=["

get", "

post"])

"/info/

", methods=["

get", "

post"])

def info(folder, filename):

fp =os.path.join(folder, filename)

return send_file(fp)

路由正則

Flask 路由系統

反向生成url def index print index print url for n1 return index 動態路由 def index nid print url for index nid 777 print url for index nid nid 根據輸入的nid,反向生成ur...

Flask 路由系統

flask中的路由系統其實我們並不陌生了,從一開始到現在都一直在應用 為什麼要這麼用?其中的工作原理我們知道多少?methods 當前 url 位址,允許訪問的請求方式 info methods get post defstudent info stu id int request.args id ...

Flask 路由系統

from flask import flask,render template login methods get post defstudent info return hello wwrd 預設不寫methods引數只支援get請求,methods引數是我們重寫裡面的方法,並不是追加 相當於給 ...