I have a view (confirm password) that sets a cookie and then redirects (302) to another view (change email).
It took me quite a lot of time, but I finally could reproduce the error I have:
Looking at the browser storage panel, one can see that the cookie is set, but at the time the second view tries to read it, there is no cookie nor value for it...
# -*- coding: utf-8 -*-
from zope.publisher.browser import BrowserView
from DateTime import DateTime
class TestView(BrowserView):
def __call__(self, *args, **kwargs):
value = self.request.cookies.get('cookie-testing')
if value is None:
new_value = '34'
else:
new_value = int(value) + 3
expires = (DateTime() + 2).toZone('GMT').rfc822()
self.request.response.setCookie(
'cookie-testing',
new_value,
path='/',
expires=expires,
)
from plone import api
self.request.response.redirect(api.portal.get().absolute_url() + '/@@test-view')
return
class TestView2(BrowserView):
def __call__(self, *args, **kwargs):
value = self.request.cookies.get('cookie-testing')
if value is None:
value = 'None'
return value
Register these two views with this ZCML:
<browser:page
for="*"
name="cookie-redirect"
permission="zope2.View"
class=".test.TestView"
/>
<browser:page
for="*"
name="test-view"
permission="zope2.View"
class=".test.TestView2"
/>
And you are good to go, call @@cookie-redirect
and see that the output on the browser is None
while the cookie is actually being set.
I tested that in external servers, so the problem seems to not be due to using localhost or the likes...
Any ideas? I'm totally puzzled... Is __call__
too early to check for cookies??