在Flask中,可以使用request.cookies
和make_response
來實現Cookie管理。
from flask import request
@app.route('/')
def index():
cookie_name = request.cookies.get('name')
return f'Hello, {cookie_name}!'
from flask import make_response
@app.route('/set-cookie')
def set_cookie():
resp = make_response('Cookie has been set!')
resp.set_cookie('name', 'Alice')
return resp
from flask import make_response
@app.route('/delete-cookie')
def delete_cookie():
resp = make_response('Cookie has been deleted!')
resp.set_cookie('name', '', expires=0)
return resp
通過以上方法,可以實現對Cookie的簡單管理和操作。