user!= users.get_current_user()

时间:2012-07-17 23:15:16

标签: google-app-engine if-statement python-2.7

更新 给出的唯一答案是帮助我。代码在我的本地系统上按预期工作,但在部署的Python应用程序中不起作用。这对我来说是个系列问题。请严肃对待这个问题。

这是一些简化的代码,用于显示正在发生的事情。当简化代码放入SDK的交互式控制台时,它总是打印出Hello, schott.brian,但在已部署的应用程序中,它会打印出no match。怎么了?

user = "schott.brian"
name = "schott.brian"

if user and user == name:
     print "Hello, " + user
else:
     print "no match"

这是我的代码。请注意第二个if支票if user and (user == person.user):

user = users.get_current_user()
ID_id = self.request.get("ID", None)
ID_id = ''.join(ID_id.split())
key = db.Key.from_path("PQ", ID_id)
person = PQ.get(key)
if person: #person's ID DOES exist already
     logging.info("6 person %s" % person.user )
     logging.info("6 key %s" % key )
     logging.info("6 ID_id %s" % ID_id )
     logging.info("6 user %s" % user )
     if user and (user == person.user):
          # never get here on gae, but works on sdk
     else:
          # always gets to here by mistake on gae, but works on SDKs
          logging.info("7 user %s" % user )
          logging.info("7 person %s" % person.user )

以下是gae上代码的日志,即使用户存在也是如此 (user == person.user),if子句不成功。你能告诉我为什么吗?这两个中的一个真的包含gmail.com而另一个没有吗?

2012-07-17 15:39:19.993 6 person schott.brian
I 2012-07-17 15:39:19.993 6 key ag1zfnBhcnRpY2lwb2xschMLEgJQUSILY29kZUJTY2hvdHQM
I 2012-07-17 15:39:19.993 6 ID_id codeBSchott
I 2012-07-17 15:39:19.993 6 user schott.brian
I 2012-07-17 15:39:19.993 7 user schott.brian
I 2012-07-17 15:39:19.994 7 person schott.brian

1 个答案:

答案 0 :(得分:7)

user.get_current_user()返回一个对象。打印用户将其转换为字符串,因此您可能希望执行以下操作:

if user and (str(user) == str(person.user)):

但实际上,您最好不要比较用户ID,而不是用户对象的字符串表示。