0

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

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>

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

也许我缺少一些东西?

编辑:

因此,在查看此页面(来自 Brython 文档的链接)https://html.spec.whatwg.org/multipage/webstorage.html后,我发现本地存储与 cookie 不同,而是存在类似的系统并排。

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

4

1 回答 1

1

所以,事实证明这很简单。该代码与 JavaScript 中的代码几乎相同。

下面是一些用于创建以foovalue命名的 cookie 的示例代码bar

from browser import document

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

就是这样!

于 2020-02-08T23:56:21.557 回答