Variables Access, get and post
how to get to these variables. This is probably quite easy, but I do not think today.
class Contact(webapp.RequestHandler):
def get(self):
self.a = random.randint(1,4)
self.b = random.randint(0,4)
template_values = {
'a': self.a,
'b': self.b
}
path = os.path.join(os.path.dirname(__file__), 'contact.html')
self.response.out.write(template.render(path, template_values))
def post(self):
self.response.out.write(self.a)
self.response.out.write(self.b)
Trackback:
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 702, in __call__
handler.post(*groups)
File "D:\My Dropbox\project\main.py", line 133, in post
self.response.out.write(self.a)
开发者_Python百科AttributeError: 'Contact' object has no attribute 'a'
Each request creates a new handler instance. For example, you can add this constructor to your handler:
class AppHandler(webapp.RequestHandler):
def __init__(self, *args, **kwargs):
logging.debug('handler "%s" created' % self)
super(AppHandler, self).__init__(*args, **kwargs)
<...>
And make two requests, then in your logs you can see something like that:
DEBUG 2011-07-10 13:36:17,009 app.py:19] handler "<__main__.AppHandler object at 0x98dad8c>" created
<...>
DEBUG 2011-07-10 13:36:52,563 app.py:19] handler "<__main__.AppHandler object at 0x98de14c>" created
If you want to get some data between requests you can try to using some kind of sessions implementation. For example this: https://github.com/dound/gae-sessions.
精彩评论