Google AppEngine Python: Class Not Defined

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('<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)

application = webapp.WSGIApplication(
  [('/', MainPage), ('/main', MainPage), ('/HugPanda', HugAPanda)],

def main():

if __name__ == "__main__":

Does anybody know why this is happening?


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?

Need Your Help

Retrieve Selection from Dropdown and Populate List

jquery element populate

I need to retrieve all of the selected values of each dropdown list on a page, and populate a unordered list with these values. A new li element per item of course.

Efficiency of Neo4j query on relationship type

indexing neo4j relationship

Say I have a graph with millions of users, and millions relationships between them.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.