W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
視圖函數(shù)的返回值會被自動轉換為一個響應對象。如果返回值是一個字符串, 它被轉換為該字符串為主體的、狀態(tài)碼為 200 OK``的 、 MIME 類型是 ``text/html 的響應對象。Flask 把返回值轉換為響應對象的邏輯是這樣:
如果你想在視圖里操縱上述步驟結果的響應對象,可以使用 make_response() 函數(shù)。
譬如你有這樣一個視圖:
@app.errorhandler(404)
def not_found(error):
return render_template('error.html'), 404
你只需要把返回值表達式傳遞給 make_response() ,獲取結 果對象并修改,然后再返回它:
@app.errorhandler(404)
def not_found(error):
resp = make_response(render_template('error.html'), 404)
resp.headers['X-Something'] = 'A value'
return resp
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: