我们可以通过 cookies 属性来访问 Cookies,用响应对象的 set_cookie 方法来设置 Cookies,并且我们要知道,请求对象的 cookies 属性是一个内容为客户端提交的所有 Cookies 的字典。
完事来看下读取cookie的案例:
from flask import request
@app.route('/')
def index():
username = request.cookies.get('username')
# use cookies.get(key) instead of cookies[key] to not get a
# KeyError if the cookie is missing.
再来看下存储 cookie的案例:
from flask import make_response
@app.route('/')
def index():
resp = make_response(render_template(...))
resp.set_cookie('username', 'the username')
return resp
通过上述代码,我们可以发现,Cookies 是设置在响应对象上的。
好啦,废话不多说,这次记录就到这里了。
如果感觉不错的话,请多多点赞支持哦。。。