与Brython一起使用cookies

2024-05-16 19:26:06 发布

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

我目前正试图和布莱顿一起处理饼干,但我遇到了一些麻烦。我在文档中找到了local_storage模块。当我使用这个模块设置cookie时,服务器找不到它们(在Djangoviews.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>

每次我 重新加载页面时,值I get是存储的,但cookie在服务器端不存在(request.COOKIES不包含键'test'

也许我遗漏了什么

编辑:

因此,在查看了这个页面(来自Brython docs的链接)https://html.spec.whatwg.org/multipage/webstorage.html之后,我发现本地存储与Cookie不是一回事,而是类似的系统并排存在

因此,我现在的问题是,我如何将Cookie与Brython一起使用,而不是使用本地存储


Tags: 模块testimportcookierequestlocalhtmlscript
1条回答
网友
1楼 · 发布于 2024-05-16 19:26:06

所以,结果很简单。代码与JavaScript中的代码几乎相同

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

from browser import document

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

就这样

相关问题 更多 >