2010-12-03 20 views
0

パスワードとユーザー名のCookieが空または偽の場合、ユーザーをリダイレクトするユーザー確認スクリプトを作成しようとしています。しかし、私が何をしても、それは常にユーザーを "/ wrong2"に送ります。 ifをチェックしても困ることはありません。これは、コードは、現時点では次のようになります。Google App Engineリダイレクトの問題

 dictionary = self.request.str_cookies 
    if hasattr(dictionary, 'password') and hasattr(dictionary, 'username'): 
     checkq = db.GqlQuery("SELECT * FROM Users WHERE username = :1 AND password = :2", dictionary['username'], dictionary['password']) 
     checkresult = checkq.get() 
     if checkresult.username and checkresult.password is None: 
      self.redirect("/wrong") 
    else: 
     self.redirect("/wrong2") 

私は、Pythonに非常に新しいですし、それを学ぼうとされており、障害がある場合、私はちょうどカントは見つけます。誰でもそれがどこにあるか見ることができますか?

+0

もちろん、「if」をチェックするのは面倒です。あなたの状態は常に 'False'と評価されます。 – geoffspear

答えて

2

hasattrを使用して、dictに特定のキーが含まれているかどうかを確認していますが、代わりにin演算子を使用する必要があります。 hasattr関数は、オブジェクトに特定の属性があるかどうかを確認するだけです。

if 'username' in self.request.cookies and 'password' in self.request.cookies: 
    # check against the datastore 

しかし、私は少し良いアプローチが空のユーザ名やパスワードは、(username = ''と思う)で聞かせて取得しないことが保証され、これを、ことだと思う:

だから、あなたの代わりに書くことができ

# will be None if the cookie is missing 
username = self.request.cookies.get('username') 
password = self.request.cookies.get('password') 

# This makes sure that a username and password were both retrieved 
# from the cookies, and that they're both NOT the empty string (because 
# the empty string evaluates to False in this context) 
if username and password: 
    # check against the datastore 
else: 
    self.redirect("/wrong2") 
関連する問題