Google AppEngine Python:类未定义

发布于 2024-12-19 01:10:47 字数 1582 浏览 0 评论 0原文

在我的 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 技术交流群。

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

发布评论

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

评论(1

離殇 2024-12-26 01:10:47

您在代码的早期定义了类 PandasHugs,但后来您:

HugForAPanda = PandaHugs(message=TheMessage)

注意到 Panda 的单数形式了吗?您想要的是

HugForAPanda = PandasHugs(message=TheMessage)

编辑:您还可以在MainPageget()方法中添加for PandasHugs in ListOfHugs: > 类。虽然从技术上讲,使用类名作为方法的局部变量没有任何问题,但它会令人困惑,并且会在 get() 方法中隐藏 PandasHugs 类。我可以建议类似 ListOfHugs 中的拥抱之类的内容吗?

You define the class PandasHugs early in your code, but later you have:

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. 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. Can I suggest something like for hug in ListOfHugs?

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