Laxman Marothiya
Laxman Marothiya

Reputation: 383

Unable to use app engine datastore

I have used the getting started guide of app engine.But I am unable to use the datastore.

While running the simple code provided in the guide, i am getting "cannot access apps data" error.

my code :

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)

I have uploaded the app on the server, but still getting the same error.

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

Upvotes: 0

Views: 285

Answers (2)

Tim Hoffman
Tim Hoffman

Reputation: 12986

For the dev server the answer is in your the last line of your stacktrace.

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

You probably have an app.yaml which has changed it's appid, over time and you haven't created a new datastore. You should show us you current app.yaml.

My guess is your error will be a little different in product, so show the trace for the error in production.

Upvotes: 2

user784435
user784435

Reputation:

I copied and pasted your code and it seems to be working (I had to change formatting) What is in your app.yaml? (I named my file aaa.py) this is what i used

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

handlers:
- url: /.*
  script: aaa.app

Upvotes: 0

Related Questions