Flask(九)——Cookie 处理

Cookie以文本文件的形式存储在客户端计算机上。 其目的是记住和跟踪与客户使用有关的数据,以获得更好的访问体验和网站统计。


Request 对象包含一个cookie的属性。它是所有cookie变量及其对应值的字典对象,客户端已发送。 除此之外,cookie还会存储其到期时间,路径和站点的域名。

在Flask中,cookies设置在响应对象上。 使用 make_response() 函数从视图函数的返回值中获取响应对象。 之后,使用响应对象的 set_cookie() 函数来存储cookie。

重读cookie很容易。 可以使用 request.cookies 属性的 get() 方法来读取cookie。

在下面的Flask应用程序中,当访问URL => / 时,会打开一个简单的表单。

@app.route('/')
def index():
    return render_template('index.html')	

这个HTML页面包含一个文本输入,完整代码如下所示

<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>Flask Cookies示例</title>
 </head>
    <body>
       <form action = "/setcookie" method = "POST">
          <p><h3>Enter userID</h3></p>
          <p><input type = 'text' name = 'name'/></p>
          <p><input type = 'submit' value = '登录'/></p>
       </form>
    </body>
 </html>

表单提交到URL => /setcookie。 关联的视图函数设置一个Cookie名称为: userID,并的另一个页面中呈现。

@app.route('/setcookie', methods = ['POST', 'GET'])
def setcookie():
   if request.method == 'POST':
        user = request.form['name']
        resp = make_response(render_template('readcookie.html'))
        resp.set_cookie('userID', user)
        return resp

readcookie.html 包含超链接到另一个函数 getcookie() 的视图,该函数读回并在浏览器中显示cookie值。

@app.route('/getcookie')
def getcookie():
    name = request.cookies.get('userID')
    return '<h1>welcome '+name+'</h1>'

完整的应用程序代码如下

from flask import Flask
from flask import render_template
from flask import request
from flask import make_response

app = Flask(__name__)


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/setcookie', methods=['POST', 'GET'])
def setcookie():
    if request.method == 'POST':
        user = request.form['name']
        resp = make_response(render_template('readcookie.html'))
        resp.set_cookie('userID', user)
        return resp


@app.route('/getcookie')
def getcookie():
    name = request.cookies.get('userID')
    print(name)
    return '<h1>welcome, ' + name + '</h1>'


if __name__ == '__main__':
    app.run(debug=True)

readcookie.html

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <p>Cookies已经设置完成 !</p>
    <div>请点击<a href="/getcookie">这里</a>查看</div>
</body>
</html>

运行该应用程序并访问URL => http://localhost:5000/,结果如下所示:


重读cookie的输出如下所示:

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值