kostas89
kostas89

Reputation: 49

Retrieving data from a datastore in GAE

I 'm trying to retrieve data from a datastore by using this (in example)

class Book(db.Model):
title = db.StringProperty()
author = db.StringProperty()
category = db.StringProperty()

e = Book(title ="LOTR",
     author = "JRR Tolkien",
     category = "Adventure")

class Result(webapp.RequestHandler):

 Book = db.GqlQuery("SELECT * FROM Book")
    for book in Book:
        self.responce.out.write('<b>%s</b>' % book.title)

Which this solution is from a google example, but have this result

    Traceback (most recent call last):
  File "/home/kostas89/google_appengine/google/appengine/tools/dev_appserver.py", line 3858, in _HandleRequest
    self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
  File "/home/kostas89/google_appengine/google/appengine/tools/dev_appserver.py", line 3792, in _Dispatch
    base_env_dict=env_dict)
  File "/home/kostas89/google_appengine/google/appengine/tools/dev_appserver.py", line 580, in Dispatch
    base_env_dict=base_env_dict)
  File "/home/kostas89/google_appengine/google/appengine/tools/dev_appserver.py", line 2918, in Dispatch
    self._module_dict)
  File "/home/kostas89/google_appengine/google/appengine/tools/dev_appserver.py", line 2822, in ExecuteCGI
    reset_modules = exec_script(handler_path, cgi_path, hook)
  File "/home/kostas89/google_appengine/google/appengine/tools/dev_appserver.py", line 2697, in ExecuteOrImportScript
    handler_path, cgi_path, import_hook)
  File "/home/kostas89/google_appengine/google/appengine/tools/dev_appserver.py", line 2621, in LoadTargetModule
    module_code = compile(source_file.read(), cgi_path, 'exec')
  File "/home/kostas89/library/library.py", line 77
    for book in Book:
   ^
IndentationError: unexpected indent

I also tried the fetch() method without any result..

EDIT: Yes but when i delete the indent, i have an error for the 'self'.

Traceback (most recent call last):
  File "/home/kostas89/google_appengine/google/appengine/tools/dev_appserver.py", line 3858, in _HandleRequest
    self._Dispatch(dispatcher, self.rfile, outfile, env_dict)
  File "/home/kostas89/google_appengine/google/appengine/tools/dev_appserver.py", line 3792, in _Dispatch
    base_env_dict=env_dict)
  File "/home/kostas89/google_appengine/google/appengine/tools/dev_appserver.py", line 580, in Dispatch
    base_env_dict=base_env_dict)
  File "/home/kostas89/google_appengine/google/appengine/tools/dev_appserver.py", line 2918, in Dispatch
    self._module_dict)
  File "/home/kostas89/google_appengine/google/appengine/tools/dev_appserver.py", line 2822, in ExecuteCGI
    reset_modules = exec_script(handler_path, cgi_path, hook)
  File "/home/kostas89/google_appengine/google/appengine/tools/dev_appserver.py", line 2702, in ExecuteOrImportScript
    exec module_code in script_module.__dict__
  File "/home/kostas89/library/library.py", line 74, in <module>
    class Result(webapp.RequestHandler):
  File "/home/kostas89/library/library.py", line 79, in Result
    self.responce.out.write('<b>%s</b>' % book.title)
NameError: name 'self' is not defined

I found that propably is because of the tabs/sapces and I try some solutions but still not working.

Upvotes: 1

Views: 1024

Answers (5)

Sam Holder
Sam Holder

Reputation: 32936

I'd remove the indent:

 Book = db.GqlQuery("SELECT * FROM Book")
 for book in Book:
    self.responce.out.write('<b>%s</b>' % book.title)

as the error suggests and see what happens. Most likely it is as is being reported:

File "/home/kostas89/library/library.py", line 77
for book in Book:
^ IndentationError: unexpected indent

indentation is significant in python code. But white space is not necessarily.

Upvotes: 0

Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26655

If want you can try reindent.py that makes sure indentation is correct or a lint tool such as pylint. I found reindent very useful and both reindent and pylint and worth trying.

Upvotes: 1

Nick Johnson
Nick Johnson

Reputation: 101139

You haven't put your code inside a method. It should look something like this:

class Result(webapp.RequestHandler):
  def get(self):
    Book = db.GqlQuery("SELECT * FROM Book")
    for book in Book:
        self.response.out.write('<b>%s</b>' % book.title)

Also:

  • Your indentation in the question doesn't seem to match what you're actually using - it's important to get this right in a language like Python, where indentation matters.
  • You misspelled 'response'.
  • This won't return any results because you haven't called .put() on the Book entity you created.

I would strongly recommend working your way through the App Engine tutorial as Drew suggests.

Upvotes: 1

Drew Sears
Drew Sears

Reputation: 12838

The properties inside your Book class should be indented. Your entity is being instanciated outside of a handler method and not being saved. Your GqlQuery is inside a handler but not inside a method, it isn't indented correctly, it re-assigns the variable you used for your model class, your for statement isn't indented correctly, and you misspelled response.

No offense, but you might want to back up and go through the App Engine Getting Started tutorial. Once you've completed those examples, make small, incremental changes and verify that the code still runs after each one.

Upvotes: 2

Chris Bunch
Chris Bunch

Reputation: 89793

Look at the error that's reported - it's telling you the right thing:

IndentationError: unexpected indent

Remove the indentation, to produce:

 Book = db.GqlQuery("SELECT * FROM Book")
 for book in Book:
   self.responce.out.write('<b>%s</b>' % book.title)

Upvotes: 1

Related Questions