flask鉤子

請求鉤子  從請求到響應的過程當中,設置了一些方法來實現某些功能
before_first_request   在處理第一個請求前運行
before_request  在每次請求前運行
after_request  在每次請求後運行
teardown_request  有未處理的異常的時候拋出
 
@app.before_request
def first():
    print('first')
 
@app.before_request
def demo2():
    print 'demo2'
 
@app.after_request
def demo3(response):
    print 'demo3'
 
    # 設置響應頭
    response.headers['Content-Type'] = 'application/json'
 
    return response
 
@app.teardown_request
def demo4(e):
    print 'demo4', e
 
裝飾器的路由的實現:Werkzeug 和Jinja2   Werkzeug實現了路由/調試/web服務器網關接口 Jinja2實現模板
Werkzeug庫的routing模塊負責實現URL解析。不一樣的URL對應不一樣的視圖函數,routing模塊會對請求信息的URL進行解析,匹配到URL對應的視圖函數,以今生成一個響應信息

設置cookie
resp = make_response('this is to set cookie')
resp.set_cookie('username', 'itcast')
獲取cookie
resp = request.cookies.get('username')
url_for flask中的反向解析
return redirect(url_for('index')) ‘index’ 是視圖函數的名字
相關文章
相關標籤/搜索