开发者

gae error:AttributeError: 'NoneType' object has no attribute 'user_is_member'

开发者 https://www.devze.com 2022-12-29 05:51 出处:网络
class Thread(db.Model): members = db.StringListProperty() def user_is_member(self, user): return str(user) in self.members
class Thread(db.Model):
  members = db.StringListProperty()

  def user_is_member(self, user):
    return str(user) in self.members

and

thread = Thread.get(db.Key.from_path('Thread', int(id)))
is_member = thread.user_is_member(user)

but the error is :

Traceback (most recent call last):
  File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 511, in __call__
    handler.get(*groups)
  File "D:\Program Files\Google\google_appengine\google\appengine\ext\webapp\util.py", line 62, in check_login
    handler_method(self, *args)
  File "D:\zjm_code\forum_blog_gae\main.py", line 222, in get
    开发者_如何学编程is_member = thread.user_is_member(user)
AttributeError: 'NoneType' object has no attribute 'user_is_member'

why ?

thanks


You're attempting to fetch an entity by key, but no entity with that key exists, so .get() is returning None. You need to check that a valid entity was returned before trying to act on it, like this:

thread = Thread.get(db.Key.from_path('Thread', int(id)))
if thread:
  is_member = thread.user_is_member(user)
else:
  is_member = False

or, equivalently:

thread = Thread.get(db.Key.from_path('Thread', int(id)))
is_member = thread.user_is_member(user) if thread else False
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号