Pregunta

I am making a small project and I have a main file, called blog.py and a separate file called users.py in a folder called source.

The layout of the project so far is:

MainFolder
    |_blog.py
    |
    |_source
        |_user.py

So, my user.py is very simple and it has a @staticmethod:

from google.appengine.ext import db

class User(db.Model):
    username = db.StringProperty(required = True)
    pwd_hash = db.StringProperty(required = True)
    email = db.StringProperty()

    @staticmethod
    def searchByUsername(anUserName):
        usersList = db.GqlQuery("select * from User where username='" + anUserName + "'")
        user = None
        for theUser in usersList:
            user = theUser
        return user 

In my blog.py file I try to call that method by making User.searchByUsername(anUserName), however when I do so everythning blows up and I end up with an error (check last 2 lines):

Traceback (most recent call last):
  File "/home/pedro/google_appengine/google/appengine/runtime/wsgi.py", line 266, in Handle
    result = handler(dict(self._environ), self._StartResponse)
  File "/home/pedro/google_appengine/lib/webapp2-2.3/webapp2.py", line 1519, in __call__
    response = self._internal_error(e)
  File "/home/pedro/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/home/pedro/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/home/pedro/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/home/pedro/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "/home/pedro/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/home/pedro/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/home/pedro/google_appengine/hw5/blog.py", line 202, in post
    user = User.searchByUsername(username)
NameError: global name 'User' is not defined

I am using from source import user in my blog.py file, and the sources folder also has the __init__.py blank folder, so I know that file is being correctly imported.

Further more, if I just copy/paste the contents of the user.py file into the blog.py file everything works fine (but the I the code gets big and not organized). how can I fix this?

¿Fue útil?

Solución

By doing from source import user, you are importing the module user, not the class User inside it. If you want to import that class, you should do from source.user import User. Or you can do from source import user and then refer to the class as user.User.

The staticmethod has nothing to do with it. As the error message suggests, the issue is that it does not know what User is, so it can't even begin to call a method of that class.

Otros consejos

Sounds like you may have forgotten to import User into blog.py:

from source.user import User
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top