如何在google app引擎模板上获取cookie值 [英] how to get cookies values on google app engine template

查看:169
本文介绍了如何在google app引擎模板上获取cookie值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序来了解python和Google App Engine。我想从Cookie获取值,并在模板上打印以隐藏或显示某些内容。



有可能吗?



什么样的会话系统最适合使用google app引擎? / p>

在gae和模板上使用会话的最佳方式是什么?



如何验证使用模板的Cookie?

解决方案

请记住,Google App Engine是一个平台,不是框架,
问题是如果webapp2(在GAE中使用的默认框架)
有一个漂亮的接口来处理cookie。即使框架没有
有这个接口,只要你有访问
的cookie头的请求,你就可以访问cookie了。



这里有两个例子,一个使用webapp2 cookies接口,
其他只使用Cookie头。



webapp2:

  class MyHandler(webapp2.RequestHandler):
def get(self):
show_alert = self.request.cookies。 get(show_alert)
...

Cookie标头(使用webapp2) / p>

 #cookies版本1不包括
def get_cookies(request):
cookies = {}
raw_cookies = request.headers.get(Cookie)
如果raw_cookies:
用于raw_cookies.split(;)中的cookie:
name,value = cookie.split =)
for name,value in cookie.split(=):
cookies [name] = value
return cookies


MyHandler(webapp2.RequestHandler):
def get(self):
cookies = get_cookies(self.request)
show_alert = cookies.get(show_alert)
...

同样适用于会话,虽然制作自己的会话库
更难, ,webapp2有你覆盖:

 来自webapp2_extras import sessions 

class MyBaseHandler(webapp2.RequestHandler):
def dispatch(self):
#为此请求获取一个会话存储
self.session_store = sessions.get_store(request = self.request)
try:
#dispatch the request
webapp2.RequestHandler.dispatch(self)
finally:
#保存所有会话
self.session_store.save_sessions(self.response)

@ webapp2.cached_property
def session(self):
#返回一个使用后端更适合你的应用程序的会话
backend =securecookie#default
backend =数据存储#使用应用程序引擎的数据存储
backend =memcache#使用应用程序引擎的内存缓存
return self.session_store.get_session(backend = backend)

class MyHandler(MyBaseHandler):
def get(self):
self.session [foo] =bar
foo = self.session.get(foo)
...

请参阅 webapp文档了解有关会话和Cookie的更多信息。



关于您的问题关于模板,您还应该查看您使用的模板引擎的文档,并查找您需要了解的内容。


I'm developing an application to learn about python and Google App Engine. I would like to get values from cookies and print on templates to hide or show some content.

Is it possible?

What kind of session system is the best to use with google app engine?

What could it be the best way to use sessions on gae and templates?

How can i validate the value of the cookies using templates?

解决方案

remember that Google App Engine is a platform, not a framework, so your question would be if webapp2 (the default framework used in GAE) has a nice interface to deal with cookies. Even if the framework doesn't have this interface, as long as you have access to the Cookie header of the request you can have access to cookies.

Here are two examples, one using the webapp2 cookies interface, and the other just using the Cookie header.

webapp2:

class MyHandler(webapp2.RequestHandler):
    def get(self):
        show_alert = self.request.cookies.get("show_alert")
        ...

Cookie header (using webapp2):

# cookies version 1 is not covered
def get_cookies(request):
    cookies = {}
    raw_cookies = request.headers.get("Cookie")
    if raw_cookies:
        for cookie in raw_cookies.split(";"):
            name, value = cookie.split("=")
            for name, value in cookie.split("="):
                cookies[name] = value
    return cookies


class MyHandler(webapp2.RequestHandler):
    def get(self):
        cookies = get_cookies(self.request)
        show_alert = cookies.get("show_alert")
        ...

The same is for sessions, although making your own session library is more difficult, anyway, webapp2 have you covered:

from webapp2_extras import sessions

class MyBaseHandler(webapp2.RequestHandler):
    def dispatch(self):
        # get a session store for this request
        self.session_store = sessions.get_store(request=self.request)
        try:
            # dispatch the request
            webapp2.RequestHandler.dispatch(self)
        finally:
            # save all sessions
            self.session_store.save_sessions(self.response)

    @webapp2.cached_property
    def session(self):
        # returns a session using the backend more suitable for your app
        backend = "securecookie" # default
        backend = "datastore" # use app engine's datastore
        backend = "memcache" # use app engine's memcache
        return self.session_store.get_session(backend=backend)

class MyHandler(MyBaseHandler):
    def get(self):
        self.session["foo"] = "bar"
        foo = self.session.get("foo")
        ...

see webapp documentation for more information about sessions and cookies.

About your question regarding the templates, again, you should look at the documentation of the template engine your using and look for what you need to know.

这篇关于如何在google app引擎模板上获取cookie值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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