2010-04-07 15 views
1

我有一個簡單的GAE應用程序,包括登錄/註銷鏈接。此應用正在dev服務器上運行。谷歌應用程序引擎:與用戶API的困難(或者可能只是一個Python語法問題)

基頁處理程序獲取當前用戶,並適當地創建登錄/註銷url。然後它將這些信息放入一個_template_data字典中,以方便子類。

​​

這裏就是這樣的一個子類:

class MainPage(BasePage): 
    def get(self): 
     self.render('start', self._template_data) 

登錄/註銷鏈接顯示的罰款,並要正確devserver登錄/註銷頁面。但是,它似乎沒有效果 - 服務器似乎仍認爲用戶已註銷。我在這裏做錯了什麼?

回答

2

我相信問題是_user屬性。

目前,_user屬性在包含該類的模塊被導入時(可能在應用程序啓動時)被綁定。您需要爲每個請求獲取當前用戶。

我會改寫成類似:

class BasePage(webapp.RequestHandler): 
    def render(self, template_name, data={}): 
     template_data = {} 
     user = template_data["user"] = users.get_current_user() 
     template_data["login_logout_link"] = users.create_logout_url() if user else users.create_login_url() 
     template_data.update(data) 
     path = os.path.join(os.path.dirname(__file__), 'Static', 'Templates', '%s.html' % template_name) 
     self.response.out.write(template.render(path, template_data) 

模板然後就會讓userlogin_logout_link在發給在子類中的值,您可以通過額外的值來使用數據參數中的模板(template_data.update(data)使用data字典中的鍵/值對更新template_data字典)。

子類例如:

class MainPage(BasePage): 
    def get(self): 
     self.render('start', data={"now": datetime.now()}) 
+0

的確。這工作。謝謝。 – 2010-04-07 19:16:43

相關問題