在 Brython 中使用 cookie

Using cookies with Brython

我目前正在尝试使用 Brython 处理 cookie,但遇到了一些问题。我在文档中找到了 local_storage 模块。当我使用这个模块设置 cookie 时,服务器找不到它们(在 Django views.py 函数中使用 request.COOKIES)。 奇怪的是,这个值被存储了,因为当我重新加载页面时,脚本会在控制台中打印它。

HTML页面中的代码:

<script type="text/python">
    from browser.local_storage import storage
    import random

    if 'test' in storage:
        print(storage['test'])
    storage['test'] = str(random.randint(0, 100))
    print(storage['test'])
</script>

每次重新加载页面时,Iget 的值都是已存储但服务器端不存在 cookie 的值(request.COOKIES 不包含密钥 'test')。

也许我遗漏了什么?

编辑:

因此,在查看此页面(link 来自 Brython 文档)https://html.spec.whatwg.org/multipage/webstorage.html 后,我发现本地存储与 cookie 不同,但它们是并存的相似系统.

所以我现在的问题是如何将 cookie 与 Brython 一起使用而不是本地存储?

所以,事实证明这很简单。代码与 JavaScript.

中的代码几乎相同

下面是一些示例代码,用于创建名为 foo 且值为 bar 的 cookie:

from browser import document

document.cookie = 'foo=bar; Path=/'

就是这样!