我正在尝试使用此方法从数据存储中检索数据 (例如)
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()方法而没有任何结果..
修改 是的但是当我删除缩进时,我对'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
我发现这可能是因为标签/ sapces,我尝试了一些解决方案,但仍然无法正常工作。
答案 0 :(得分:2)
Book类中的属性应缩进。您的实体正在处理程序方法之外进行实例化而不是保存。你的GqlQuery在一个处理程序中,但不在方法内,它没有正确缩进,它重新分配你用于模型类的变量,你的for语句没有正确缩进,你拼错了响应。
没有冒犯,但您可能需要备份并完成App Engine Getting Started教程。完成这些示例后,进行小的增量更改并验证代码是否仍然在每个示例之后运行。
答案 1 :(得分:1)
查看报告的错误 - 它告诉你正确的事情:
IndentationError: unexpected indent
删除缩进,以产生:
Book = db.GqlQuery("SELECT * FROM Book")
for book in Book:
self.responce.out.write('<b>%s</b>' % book.title)
答案 2 :(得分:1)
您尚未将代码放入方法中。看起来应该是这样的:
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)
另外:
.put()
实体上调用Book
。我强烈建议按照Drew建议的方式通过App Engine教程。
答案 3 :(得分:1)
如果您愿意,可以尝试reindent.py确保缩进正确或使用pylint等lint工具。我发现reindent非常有用,无论是redent还是pylint都值得尝试。
答案 4 :(得分:0)
我会删除缩进:
Book = db.GqlQuery("SELECT * FROM Book")
for book in Book:
self.responce.out.write('<b>%s</b>' % book.title)
正如错误所示,看看会发生什么。最有可能是正如报道的那样:
python代码中的文件“/home/kostas89/library/library.py”,第77行 书中的书:
^ IndentationError:意外缩进
indentation is significant。但是white space is not necessarily。