2011-07-10 24 views
-2

如何获得这些变量。 这可能很容易,但我今天不认为。变量访问,获取和发布

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) 

引用:

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) 
AttributeError: 'Contact' object has no attribute 'a' 
+0

错误发生在哪里?在'后'?你在这里试图做什么? –

+0

是的在后,简单的数学验证码 –

+1

我们喜欢看到完整的回溯。 –

回答

4

每个请求创建一个新的处理程序实例。例如,您可以将此构造函数添加到您的处理程序:

class AppHandler(webapp.RequestHandler): 
    def __init__(self, *args, **kwargs): 
     logging.debug('handler "%s" created' % self) 
     super(AppHandler, self).__init__(*args, **kwargs) 
    <...> 

并提出两个请求,那么在您的日志中可以看到类似的东西:

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 

如果你想获得之间的一些数据请求您可以尝试使用某种会话实现。例如:https://github.com/dound/gae-sessions