Google AppEngine Python:类未定义
在我的 Python 编码的 AppEngine 应用程序中,我收到以下错误代码:
NameError:未定义全局名称“PandaHugs”
我不明白为什么,因为我在调用它的地方上方定义了“PandaHugs”。这是代码:
#!C:\Python25\python.exe -u
import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class PandasHugs(db.Model):
message = db.StringProperty(required=False, multiline=False)
class MainPage(webapp.RequestHandler):
def get(self):
ListOfHugs = db.GqlQuery("SELECT * FROM PandasHugs")
Adder = 0
for PandasHugs in ListOfHugs:
Adder = Adder + 1
self.response.out.write('<html><body>')
self.response.out.write('<h6>Panda has ' + str(Adder) + ' hugs!</h6>')
self.response.out.write("<form action=\"/HugPanda\" method=\"post\"><div><input type=\"text\" name=\"PandaMessage\" value=\"A message for a panda.\"></div><div><input type=\"submit\" value=\"Hug a panda?\"></div></form></body></html>")
class HugAPanda(webapp.RequestHandler):
def post(self):
TheMessage = self.request.get('PandaMessage')
HugForAPanda = PandaHugs(message=TheMessage)
HugForAPanda.put()
self.redirect('/main')
application = webapp.WSGIApplication(
[('/', MainPage), ('/main', MainPage), ('/HugPanda', HugAPanda)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
有人知道为什么会发生这种情况吗?
In my Python-coded AppEngine application, I'm getting the following error code:
NameError: global name 'PandaHugs' is not defined
I can't figure out why, as I define 'PandaHugs' above the place where it is called. Here's the code:
#!C:\Python25\python.exe -u
import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class PandasHugs(db.Model):
message = db.StringProperty(required=False, multiline=False)
class MainPage(webapp.RequestHandler):
def get(self):
ListOfHugs = db.GqlQuery("SELECT * FROM PandasHugs")
Adder = 0
for PandasHugs in ListOfHugs:
Adder = Adder + 1
self.response.out.write('<html><body>')
self.response.out.write('<h6>Panda has ' + str(Adder) + ' hugs!</h6>')
self.response.out.write("<form action=\"/HugPanda\" method=\"post\"><div><input type=\"text\" name=\"PandaMessage\" value=\"A message for a panda.\"></div><div><input type=\"submit\" value=\"Hug a panda?\"></div></form></body></html>")
class HugAPanda(webapp.RequestHandler):
def post(self):
TheMessage = self.request.get('PandaMessage')
HugForAPanda = PandaHugs(message=TheMessage)
HugForAPanda.put()
self.redirect('/main')
application = webapp.WSGIApplication(
[('/', MainPage), ('/main', MainPage), ('/HugPanda', HugAPanda)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
Does anybody know why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在代码的早期定义了类
PandasHugs
,但后来您:注意到 Panda 的单数形式了吗?您想要的是
编辑:您还可以在
MainPageget()
方法中添加for PandasHugs in ListOfHugs:
> 类。虽然从技术上讲,使用类名作为方法的局部变量没有任何问题,但它会令人困惑,并且会在get()
方法中隐藏PandasHugs
类。我可以建议类似ListOfHugs 中的拥抱
之类的内容吗?You define the class
PandasHugs
early in your code, but later you have:Notice the singular form of Panda? What you want is
Edit: You also have
for PandasHugs in ListOfHugs:
in theget()
method of yourMainPage
class. While there is technically nothing wrong with using the class name as a local variable of your method, it is confusing and hides thePandasHugs
class in theget()
method. Can I suggest something likefor hug in ListOfHugs
?