在教程中检索已提交的问候语

时间:2013-05-22 04:39:55

标签: python google-app-engine google-cloud-datastore

我正在使用GAE教程,我收到以下错误。

文件“/Users/cparrish/bin/guestbook/guestbook.py”,第62行,在获取     在问候中问候: NameError:未定义全局名称'greetings'

所以我觉得问题就在这里。

 greetings_query = Greeting.query(
                     ancestor = guestbook_key(guestbook_name)).order(-Greeting.date)

    greeetings = greetings_query.fetch(10)

    for greeting in greetings:
        if greeting.author:
            self.response.write(
                    '<b>%s</b> wrote:' % greeting.author.nickname())

第58-66行 所以我想知道是否有其他人可以看到我在这里显然缺少的东西。

以下完整代码库。

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:
        <input value="%s" name="guestbook_name">
        <input type="submit" value="switch">
    </from>

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


    </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 signle 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 consisten with the High
        # Replication Datastore. Queries that span entity groups are eventually
        # consisten. If we omitted the ancestor from this query there would be
        # a slight chance that Greetings that had just been written would not
        # show up in a query. 
        greetings_query = Greeting.query(
                         ancestor = guestbook_key(guestbook_name)).order(-Greeting.date)

        greeetings = 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_parms = urllib.urlencode({'guestbook_name': guestbook_name})
            self.response.write(MAIN_PAGE_FOOTER_TEMPLATE % (sign_query_parms, cgi.escape(guestbook_name), url, url_linktext))

class Guestbook(webapp2.RequestHandler):

        def post(self):
            # We set the same parent key on the 'Greeting' to ensure each getting
            # is in the same entity group. Queries across the single entity group
            # will be consistent. However, the write reate to a sing entity groupo
            # 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)

1 个答案:

答案 0 :(得分:3)

e的作业中有三greeetings个。{/ p>

greeetings = greetings_query.fetch(10)

for greeting in greetings:

其中一个e与其他e不同,其中一个{{1}}只是不属于......