0

我已经使用了应用引擎的入门指南。但是我无法使用数据存储区。

在运行指南中提供的简单代码时,我收到“无法访问应用程序数据”错误。

我的代码:

import cgi
import datetime
import urllib
import webapp2

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


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"></form>
</body>
</html>
"""

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


def guestbook_key(guestbook_name=None):
"""Constructs a Datastore key for a Guestbook entity with guestbook_name."""
return db.Key.from_path('Guestbook', guestbook_name or 'default_guestbook')


class MainPage(webapp2.RequestHandler):

def get(self):
    self.response.write('<html><body>')
    guestbook_name = self.request.get('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 = db.GqlQuery("SELECT * "
                            "FROM Greeting "
                            "WHERE ANCESTOR IS :1 "
                            "ORDER BY date DESC LIMIT 10",
                            guestbook_key(guestbook_name))

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

    # 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)))


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')
    greeting = Greeting(parent=guestbook_key(guestbook_name))

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

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

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


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

我已经在服务器上上传了应用程序,但仍然遇到同样的错误。

Traceback (most recent call last):
  File "/home/laxman/google_appengine/lib/webapp2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
  File "/home/laxman/google_appengine/lib/webapp2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
  File "/home/laxman/google_appengine/lib/webapp2/webapp2.py", line 1278, in                  default_dispatcher
 return route.handler_adapter(request, response)
File "/home/laxman/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
 return handler.dispatch()
File "/home/laxman/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/home/laxman/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/home/laxman/helloworld/helloworld.py", line 51, in get
for greeting in greetings:
File "/home/laxman/google_appengine/google/appengine/ext/db/__init__.py", line 2326, in  next
 return self.__model_class.from_entity(self.__iterator.next())
File "/home/laxman/google_appengine/google/appengine/datastore/datastore_query.py", line  2892, in next
 next_batch = self.__batcher.next()
File "/home/laxman/google_appengine/google/appengine/datastore/datastore_query.py", line  2754, in next
 return self.next_batch(self.AT_LEAST_ONE)
File "/home/laxman/google_appengine/google/appengine/datastore/datastore_query.py", line 2791, in next_batch
 batch = self.__next_batch.get_result()
File "/home/laxman/google_appengine/google/appengine/api/apiproxy_stub_map.py", line 604, in get_result
return self.__get_result_hook(self)
File "/home/laxman/google_appengine/google/appengine/datastore/datastore_query.py", line   2528, in __query_result_hook
 self._batch_shared.conn.check_rpc_success(rpc)
File "/home/laxman/google_appengine/google/appengine/datastore/datastore_rpc.py", line 1224, in check_rpc_success
 raise _ToDatastoreError(err)
BadRequestError: app "dev~helloworld" cannot access app "dev~hellolxmn"'s data
4

2 回答 2

2

对于开发服务器,答案在堆栈跟踪的最后一行。

app "dev~helloworld" cannot access app "dev~hellolxmn"'s data

您可能有一个 app.yaml,它随着时间的推移改变了它的 appid,而您还没有创建新的数据存储。你应该向我们展示你当前的 app.yaml。

我的猜测是您的错误在产品中会有所不同,因此请显示生产中错误的跟踪。

于 2013-04-17T00:07:20.067 回答
0

我复制并粘贴了您的代码,它似乎可以正常工作(我不得不更改格式)您的 app.yaml 中有什么?(我将我的文件命名为 aaa.py)这是我使用的

application: somename
version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: aaa.app
于 2013-04-16T19:10:10.137 回答