您如何在Zope和Plone中获取和设置Cookie? [英] How do you get and set cookies in Zope and Plone?

查看:79
本文介绍了您如何在Zope和Plone中获取和设置Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档,更重要的是,一些代码示例将非常有用.我希望这不是在受保护的脚本中,而是在进入现代程序包的代码中.

Documentation, and more importantly, some code examples would be very useful. I would prefer this to not be in protected scripts, but in the code that goes into modern packages.

推荐答案

使用response.setCookie()方法.您可以通过请求对象到达响应对象.您可以通过获取(self.REQUEST)或通常通过self.request:

Use the response.setCookie() method. You can reach the response object via the request object. The latter you can reach via acquisition (self.REQUEST), or in views by accessing the passed-in request object, usually via self.request:

self.request.response.setCookie(name, value, **options)

其中,options最终作为额外的cookie参数.因此,将cookie变成非会话cookie需要使用expires='date'关键字,将cookie限制为路径是setCookie()方法的path='/somepath'关键字.通常的浏览器cookie规则在这里适用.

where options end up as extra cookie parameters. Thus, turning a cookie into a non-session cookie requires a expires='date' keyword, limiting the cookie to a path is a path='/somepath' keyword to the setCookie() method. The usual browser cookie rules apply here.

要使已经在浏览器中设置的cookie失效,可以使用expires='date in the past'关键字,也可以使用response.expireCookie()方法,该方法可以为您完成此操作:

To expire a cookie already set in the browser, you could either use a expires='date in the past' keyword, or you could use the response.expireCookie() method, which does this for you:

self.request.response.expireCookie(name, **options)

在这种情况下,您仍然可以包括path或其他cookie标志之类的选项,但是该方法将覆盖max_ageexpires选项,以确保该cookie被浏览器删除.

In this case you can still include options like the path or other cookie flags, but the method will override the max_age and expires options to ensure the cookie is deleted by the browser.

尽管您可以使用Zope的SESSION支持,但您确实需要仔细考虑可伸缩性问题.例如,如果您使用ZEO或RelStorage,则需要考虑如何在群集之间共享会话数据.如果可伸缩性将成为一个问题,通常最好完全避免使用SESSION.

Although you could use Zope's SESSION support, you really need to think through the scalability issues. For example, you need to think through how session data will be shared across a cluster if you use ZEO or RelStorage. It is generally preferable to avoid using SESSION altogether if scalability is going to be an issue.

这篇关于您如何在Zope和Plone中获取和设置Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆