从 GAE 中的数据存储检索数据
我正在尝试使用此方法从数据存储中检索数据 (在示例中)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
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.
查看报告的错误 - 它告诉您正确的事情:
删除缩进,以生成:
Look at the error that's reported - it's telling you the right thing:
Remove the indentation, to produce:
您还没有将代码放入方法中。它应该看起来像这样:
另外:
Book
实体调用.put()
。我强烈建议您按照 Drew 的建议完成 App Engine 教程。
You haven't put your code inside a method. It should look something like this:
Also:
.put()
on theBook
entity you created.I would strongly recommend working your way through the App Engine tutorial as Drew suggests.
如果需要,您可以尝试 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.
我会删除缩进:
如错误所示,看看会发生什么。最有可能的是,正如所报道的那样:
Python 代码中的缩进很重要。但空格不一定。
I'd remove the indent:
as the error suggests and see what happens. Most likely it is as is being reported:
indentation is significant in python code. But white space is not necessarily.