简体   繁体   English

Google AppEngine Python:未定义的类

[英]Google AppEngine Python: Class Not Defined

In my Python-coded AppEngine application, I'm getting the following error code: 在我的Python编码的AppEngine应用程序中,我收到以下错误代码:

NameError: global name 'PandaHugs' is not defined NameError:未定义全局名称“PandaHugs”

I can't figure out why, as I define 'PandaHugs' above the place where it is called. 我不知道为什么,因为我在调用它的地方上方定义了“ PandaHugs”。 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? 有人知道为什么会这样吗?

You define the class PandasHugs early in your code, but later you have: 您在代码的早期定义了PandasHugs类,但后来有了:

HugForAPanda = PandaHugs(message=TheMessage)

Notice the singular form of Panda? 注意熊猫的单数形式? What you want is 你想要的是什么

HugForAPanda = PandasHugs(message=TheMessage)

Edit: You also have for PandasHugs in ListOfHugs: in the get() method of your MainPage class. 编辑:您还可以for PandasHugs in ListOfHugs:get()的方法MainPage类。 While there is technically nothing wrong with using the class name as a local variable of your method, it is confusing and hides the PandasHugs class in the get() method. 虽然使用类名作为方法的局部变量在技术上没有任何问题,但它很容易混淆并隐藏了get()方法中的PandasHugs类。 Can I suggest something like for hug in ListOfHugs ? 我可以for hug in ListOfHugs提出类似for hug in ListOfHugs吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM