Flask-API
redirect()
def redirect(location, code=302, Response=None):
重定向到其他視圖函數
# 重定向的目標視圖函數
@blue.route('/user/login/')
def login():
......
# 方式一 直接指定路由地址跳轉
@blue.route('/index/')
def index():
return redirect('/user/login/')
# 方式二 使用url_for構造路由地址跳轉
@blue.route('/index/')
def index():
return redirect(url_for(blue.login))
url_for()
構造URL
url_for('視圖函數名',key1=value1,key2=value2) # 構造當前藍圖的url
url_for('.視圖函數名',key1=value1,key2=value2) # 構造當前藍圖的url
url_for('藍圖名.視圖函數名',key1=value1,key2=value2) # 構造指定藍圖的url
- key=value將作爲構造的url的參數
- 構造的URL相當於
視圖函數的路由地址/?key1=value1&key2=value2
接收url_for()傳來的參數
# url_for('index',id=2)相當於 index視圖函數的路由地址/?id=2
# 方式一 編寫路由規則接收
@blue.route('/index/<int:id>')
def index(id):
print(id)
......
# 方式二 使用request.args.get('參數名')接收
@blue.route('/index/')
def index():
print(request.args.get('id'))
......
加載靜態文件
url_for('static',filename='image/background.jpg')
# 構造的URL:/static/image/background.jpg
- static是存放靜態文件的文件夾名。文件系統會自動搜索該static文件夾,如果
靜態文件夾名不爲static
,系統將無法搜索到
靜態文件夾。 - filename是static靜態文件夾下靜態文件的具體路徑