2013-08-23 28 views
1

不知道如果在標題上我應該說父母的價值觀或祖先,但這是我想要做的。如何使用python列出應用程序引擎數據存儲父級值?

我正在學習在Google App Engine中使用Python。在我閱讀Guestbook教程中的this page的內容時,我想知道是否可以通過列出所有可能的留言簿來增強它。

爲了給出一點上下文,這是示例代碼的工作原理。該應用程序呈現一個頁面,允許您創建留言簿條目,以及爲當前或未來條目切換/創建新留言簿。我認爲增加能夠列出當前存儲的所有留言簿以動態生成鏈接列表來查看每個留言簿會很簡單。

我認爲這很簡單,但我正在嘗試,我無法弄清楚。如何查詢數據存儲以給我所有可用的「留言簿」列表,以便我可以動態構建鏈接?如果留言簿名爲guestbook_2,則url看起來像這樣「?guestbook_name = guestbook_2」。

這裏是應用程序的代碼(我添加字符串「INSERT在此列出」在這裏我想ADDD我剛纔提到的鏈接列表):

import cgi 
import urllib 

from google.appengine.api import users 
from google.appengine.ext import ndb 

import webapp2 


MAIN_PAGE_FOOTER_TEMPLATE = """\ 
<form action="/sign?%s" method="post"> 
    <div><textarea name="content" rows="3" cols="60"></textarea></div> 
    <div><input type="submit" value="Sign Guestbook"></div> 
</form> 

<hr> 

<form>Guestbook name (you can create your own guestbook here): 
    <input value="%s" name="guestbook_name"> 
    <input type="submit" value="switch"> 
</form> 

<a href="%s">%s</a> 

<br><br> 
List of guestbooks: %s 

    </body> 
</html> 
""" 

DEFAULT_GUESTBOOK_NAME = 'default_guestbook' 


# We set a parent key on the 'Greetings' to ensure that they are all in the same 
# entity group. Queries across the single entity group will be consistent. 
# However, the write rate should be limited to ~1/second. 

def guestbook_key(guestbook_name=DEFAULT_GUESTBOOK_NAME): 
    """Constructs a Datastore key for a Guestbook entity with guestbook_name.""" 
    return ndb.Key('Guestbook', guestbook_name) 


class Greeting(ndb.Model): 
    """Models an individual Guestbook entry with author, content, and date.""" 
    author = ndb.UserProperty() 
    content = ndb.StringProperty(indexed=False) 
    date = ndb.DateTimeProperty(auto_now_add=True) 


class MainPage(webapp2.RequestHandler): 

    def get(self): 
     self.response.write('<html><body>') 
     guestbook_name = self.request.get('guestbook_name', 
              DEFAULT_GUESTBOOK_NAME) 

     # Ancestor Queries, as shown here, are strongly consistent with the High 
     # Replication Datastore. Queries that span entity groups are eventually 
     # consistent. If we omitted the ancestor from this query there would be 
     # a slight chance that Greeting that had just been written would not 
     # show up in a query. 
     greetings_query = Greeting.query(
      ancestor=guestbook_key(guestbook_name)).order(-Greeting.date) 
     greetings = greetings_query.fetch(10) 

     for greeting in greetings: 
      if greeting.author: 
       self.response.write(
         '<b>%s</b> wrote:' % greeting.author.nickname()) 
      else: 
       self.response.write('An anonymous person wrote:') 
      self.response.write('<blockquote>%s</blockquote>' % 
           cgi.escape(greeting.content)) 

     if users.get_current_user(): 
      url = users.create_logout_url(self.request.uri) 
      url_linktext = 'Logout' 
     else: 
      url = users.create_login_url(self.request.uri) 
      url_linktext = 'Login' 


     # Write the submission form and the footer of the page 
     sign_query_params = urllib.urlencode({'guestbook_name': guestbook_name}) 
     self.response.write(MAIN_PAGE_FOOTER_TEMPLATE % 
          (sign_query_params, 
          cgi.escape(guestbook_name), 
          url, 
          url_linktext, 
          "INSERT LIST HERE" 
          )) 


class Guestbook(webapp2.RequestHandler): 

    def post(self): 
     # We set the same parent key on the 'Greeting' to ensure each Greeting 
     # is in the same entity group. Queries across the single entity group 
     # will be consistent. However, the write rate to a single entity group 
     # should be limited to ~1/second. 
     guestbook_name = self.request.get('guestbook_name', 
              DEFAULT_GUESTBOOK_NAME) 
     greeting = Greeting(parent=guestbook_key(guestbook_name)) 

     if users.get_current_user(): 
      greeting.author = users.get_current_user() 

     greeting.content = self.request.get('content') 
     greeting.put() 

     query_params = {'guestbook_name': guestbook_name} 
     self.redirect('/?' + urllib.urlencode(query_params)) 


application = webapp2.WSGIApplication([ 
    ('/', MainPage), 
    ('/sign', Guestbook), 
], debug=True) 

回答

0

只是存儲的留言名稱某處,或最簡單的事情是你可以獲取所有問候的主要獲取父,像

guestbooks = [greeting.key().parent().name() for greeting in Greeting.all(keys_only=True)] 
+0

我想,這大概是我一直在尋找的答案。不過,我必須比我想象的更加綠色。你能給我更多的指導方法嗎?我知道我聲明瞭像你所建議的變量留言簿,然後我需要在需要顯示其值的地方使用它。我只是不清楚如何去做。我知道我很接近。 – Rafael

+0

爲了清楚起見,我嘗試在MainPage類的「get」方法內添加「guestbooks」變量。我也在其他地方嘗試過,但是當我嘗試這樣做時,出現了不同類型的錯誤。我不在這裏發佈錯誤,因爲我想正確處理它,而不是通過試驗和錯誤。我覺得我幾乎可以得到它。還不完全。感謝您的建議和幫助。 – Rafael

相關問題