2012-06-02 37 views
0

我可以使用函數從全局字典變量中讀寫數據。GAE Python - 對視圖元素使用全局變量字典

dic = { 'someKey' : 'someVal' } 
def f(): 
    dic['newKey'] = 'newVal' 
    print dic['someKey'] 

我想要做同樣的事情在GAE的Python模塊

class MainPage(webapp.RequestHandler): 
    def get(self): 
     user = users.get_current_user() 
     if not user: 
      viewValues = {} 
      viewValues['login'] = (users.create_login_url(self.request.path)) 
      self.response.out.write(template.render('view/login.html', viewValues)) 
     else: 
      self.redirect("/") 

    viewValues = {'promotion' : [ 
           {'icon': 'time_ico.png', 
           'heading': 'Instant feedback.', 
           'detail': 'Quiz your students and get instant results!</br>No need to waste time checking answers manually.'}, 
           {'icon': 'money_ico.png', 
           'heading': 'No equipment necessary!', 
           'detail': 'No need to spend money on special equipment.</br>Use studyWise&copy; free of charge now.'}, 
           {'icon': 'cell_ico.png', 
           'heading': 'Accessible.', 
           'detail': 'Can be used with any smartphone or laptop, no installation required.'}, 
           {'icon': 'statistics_ico.png', 
           'heading': 'Statistics.', 
           'detail': 'Get helpful info about your students understanding of the learning material today.'} 
          ] 
       } 


Traceback (most recent call last): 
    File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\_webapp25.py", line 701, in __call__ 
    handler.get(*groups) 
    File "C:\Current Project\StudentVoice_Refactor\src\login.py", line 14, in get 
    viewValues['login'] = (users.create_login_url(self.request.path)) 
NameError: global name 'viewValues' is not defined 

內爲什麼會出現這種情況? 感謝

+1

哦,等等,讓我猜,我應該使用:self.viewValues?我對Python很陌生,所以我很抱歉問一些如此簡單的東西。 – zehelvion

回答

0

說,在Python實現的方法是通過調用self.varName

在AS3/Java的,你可以去任何一種方式AFAIK,你可以寫成員名稱或this.memberName

但是在Python你不能,必須使用self.memberName而不是memberName。

那麼,猜測解決它。 :)