从 GAE 中的数据存储检索数据

发布于 2024-12-04 07:48:25 字数 3094 浏览 2 评论 0原文

我正在尝试使用此方法从数据存储中检索数据 (在示例中)

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)

这个解决方案来自谷歌示例,但是得到这个结果

    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

我也尝试了 fetch() 方法,但没有任何结果。

编辑: 是的,但是当我删除缩进时,“自我”出现错误。

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 '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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

茶花眉 2024-12-11 07:48:25

Book 类中的属性应该缩进。您的实体正在处理程序方法之外实例化并且未保存。您的 GqlQuery 位于处理程序内部,但不在方法内部,它没有正确缩进,它重新分配您用于模型类的变量,您的 for 语句没有正确缩进,并且您的响应拼写错误。

无意冒犯,但您可能需要备份并完成 App Engine 入门 教程。完成这些示例后,进行小的增量更改并验证代码在每个更改后是否仍然运行。

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.

于我来说 2024-12-11 07:48:25

查看报告的错误 - 它告诉您正确的事情:

IndentationError: unexpected indent

删除缩进,以生成:

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

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)
灰色世界里的红玫瑰 2024-12-11 07:48:25

您还没有将代码放入方法中。它应该看起来像这样:

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)

另外:

  • 问题中的缩进似乎与您实际使用的内容不匹配 - 在像 Python 这样的语言中正确处理这一点很重要,因为缩进很重要。
  • 您拼错了“响应”。
  • 这不会返回任何结果,因为您尚未对创建的 Book 实体调用 .put()

我强烈建议您按照 Drew 的建议完成 App Engine 教程。

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.

深居我梦 2024-12-11 07:48:25

如果需要,您可以尝试 reindent.py 确保缩进是正确的或 lint 工具,例如 pylint。我发现 reindent 非常有用,reindent 和 pylint 都值得尝试。

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.

仙女山的月亮 2024-12-11 07:48:25

我会删除缩进:

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

如错误所示,看看会发生什么。最有可能的是,正如所报道的那样:

文件“/home/kostas89/library/library.py”,第 77 行
对于书中的书:
^ IndentationError:意外缩进

Python 代码中的缩进很重要。但空格不一定

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文