在python中引用字符串或列表全局变量

时间:2013-11-30 14:45:43

标签: python google-app-engine web global tornado

python 2.7.2 + tornado 3.1.1 中,如何使用全局变量?我需要它在AppEngine上工作。

class LoginHandler(BaseHandler):
    def get(self):
        if self.get_current_user():
            self.redirect('/')
            return
        # assign self.get_argument('next', '/')) to the variable next_page
        self.render('login.html')

    def post(self):
        self.set_secure_cookie("user", self.get_argument("username"))
        # direct to next_page

2 个答案:

答案 0 :(得分:2)

您不需要商店next_page变量的全局变量。尝试使用session或memcache。

from google.appengine.api import memcache

# setup a key/value
memcache.set(key='next_page_%s' % user.id, next_page)

# get next_page
next_page = memcache.get(key='next_page_%s' % user.id)

我们将'next_page_%s' % user.iduser.id一起用于unigue密钥,否则每个用户只有一个next_page

答案 1 :(得分:1)

您需要将变量指定为每个函数中的全局变量:

global globalvariable

有关此问题的详情,另请参阅Using global variables in a function other than the one that created them

相关问题