2011-07-10 26 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