1. 程式人生 > 其它 >python-flask響應物件和請求物件

python-flask響應物件和請求物件

請求物件(Response):客戶端傳送給伺服器端的資料(瀏覽器---伺服器)

Response的屬性:

1、request.method:獲取請求的方法

2、request.args: 獲取URL的查詢引數

3、request.args.to_dict():查詢引數轉化為dict

4、request.form:獲取FormDage中的檔案資料

5、request.values:獲取Form和Args中的資料

6、request.path:路由地址

7、request.full_path:獲取URL的全路徑

8、request.url:獲取訪問的全路徑

9、request.cookies:獲取cookies中的資料

10、request.headers:獲取請求頭中的資料

響應物件(request):伺服器端傳送給客戶端的資料(伺服器---瀏覽器)

Request:

1、響應字串

@app.route("/")
def login():
    return "歡迎"

2、返回模板檔案

from flask import render_template
@app.route("/")
def index():
    return render_template("html模板路徑")

3、重定向

from flask import redirect
@app.route("/")
def login():
    return redirect('/index.html')

4、製造響應物件

from flask import make_response, render_template
# 製造一個相應物件並返回一個模板檔案
@app.route("/")
def login():
  
  response = make_response(render_template('index.html'))

5、其他響應方法

response.delete_cookie('key')  # 刪除cookie某個鍵
response.set_cookie('key', 'value')  # 設定cookie的鍵
response.headers['X-Something'] = 'A value'  # 設定響應頭資訊
....