Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 谷歌应用引擎重定向问题_Python_Google App Engine_Google Cloud Datastore - Fatal编程技术网

Python 谷歌应用引擎重定向问题

Python 谷歌应用引擎重定向问题,python,google-app-engine,google-cloud-datastore,Python,Google App Engine,Google Cloud Datastore,我正在尝试创建一个用户验证脚本,如果密码和用户名cookie为空或为假,该脚本将重定向用户。但无论我做什么,它总是将用户发送到“/错误2”。它甚至不需要检查if。这就是代码目前的样子: dictionary = self.request.str_cookies if hasattr(dictionary, 'password') and hasattr(dictionary, 'username'): checkq = db.GqlQuery("SELEC

我正在尝试创建一个用户验证脚本,如果密码和用户名cookie为空或为假,该脚本将重定向用户。但无论我做什么,它总是将用户发送到“/错误2”。它甚至不需要检查if。这就是代码目前的样子:

        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非常陌生,正在尝试学习它,但我就是找不到问题所在。有人能看到它在哪里吗?

您正在使用
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")

当然,如果,它会麻烦地检查
。您的情况总是计算为
False