Flask - 使用 Lis 设置 Cookies

2024-10-01 07:48:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Flask设置一个与列表相等的cookie

这就是我想要达到的目标

resp = make_response(render_template('home.html'))
cookiePayload = [userName, rights]
resp.set_cookie('username', cookiePayload)
return resp

但它给我带来了一些打字错误。怎么了


Tags: flask目标home列表makecookieresponsehtml
1条回答
网友
1楼 · 发布于 2024-10-01 07:48:27

以下是set_cookie方法的文档:

Sets a cookie. The parameters are the same as in the cookie Morsel object in the Python standard library but it accepts unicode data, too.

https://flask.palletsprojects.com/en/1.1.x/api/#flask.Response.set_cookie

因此,您需要将这些列表转换为字符串。实现这一点的方法之一是将列表转换为json对象

让我们看看行动

import json


resp = make_response(render_template('home.html'))
cookiePayload = [userName, rights]
resp.set_cookie('username', json.dumps(cookiePayload))
return resp

相关问题 更多 >