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?
You define the class
PandasHugsearly 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 yourMainPageclass. While there is technically nothing wrong with using the class name as a local variable of your method, it is confusing and hides thePandasHugsclass in theget()method. Can I suggest something likefor hug in ListOfHugs?