-2

In python 2.7.2 + tornado 3.1.1, how to use a global variable? I need it work on 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
Gray
  • 79
  • 1
  • 9

2 Answers2

2

You don't need global variables for store next_page variable. Try to use sessions or memcache for it.

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)

We use 'next_page_%s' % user.id with user.id for unigue key, otherwise every user will have only one next_page.

greg
  • 1,417
  • 9
  • 28
1

You need to assign the variable as being global within each function:

global globalvariable

See also Using global variables in a function other than the one that created them for more information on this issue.

Community
  • 1
  • 1
fuesika
  • 3,280
  • 6
  • 26
  • 34