Hello! How to set cookie in werkzeug from any def in code. I know that
cookie sets by using Response.set_cookie, but (code bellow) Response is in
the wsgi_app, and i need set cookie from def set_cookie
class app():
def __init__():
self.local = Local()
self.local_manager = LocalManager([self.local])
self.url_map = Map([
Rule('/set_cookie', endpoint = 'set_cookie'),
def set_cookie(self, request):
self.local.response.set_cookie('cookie', 'cookie', domain='.XXX.com') #
This is not Work
return render_template(Bla bla bla)
def set_cookie2(self, request):
self.local.cookie = 'Cookie' # This works But wsgi_app is ugly)
return render_template(Bla bla bla)
def ep_logout(self, request):
self.local.cookie = 'dc' # This is context local to delete cookie, see
wsgi_app
return redirect(request.args['continue'], 302)
def dispatch_request(self, request):
adapter = self.url_map.bind_to_environ(request.environ)
try:
endpoint, values = adapter.match()
return getattr(self, endpoint)(request, **values)
except NotFound, e:
return self.error_404()
except HTTPException, e:
return e
# Main app
def wsgi_app(self, environ, start_response):
self.local.request = request = Request(environ)
self.local.response = response = self.dispatch_request(request)
if hasattr(self.local, 'cookie') and 'dc' not in self.local.cookie:
response.set_cookie('uid', self.local.cookie, max_age = 31536000,
expires = utils.cookie_expires(), domain='.XXX.com')
if hasattr(self.local, 'cookie') and 'dc' in self.local.cookie:
response.delete_cookie('uid', domain='.XXX.com')
sid = request.cookies.get('uid')
if sid is None:
request.session = self.session_store.new()
else:
request.session = self.session_store.get(sid)
if request.session.should_save:
self.session_store.save(request.session)
response.set_cookie('uid', request.session.sid)
return response(environ, start_response)
--
You received this message because you are subscribed to the Google Groups
"pocoo-libs" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/pocoo-libs/-/ud6QxDAD_UYJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pocoo-libs?hl=en.