Google App Engine 重定向问题

发布于 2024-10-06 03:02:03 字数 623 浏览 0 评论 0原文

我正在尝试制作一个用户验证脚本,如果密码和用户名 cookie 为空或错误,则该脚本会重定向用户。但无论我做什么,它总是将用户发送到“/wrong2”。它甚至懒得检查 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 很陌生,正在尝试学习它,但我找不到问题所在。谁能看到它在哪里吗?

I'm trying to make a user verification script that redirects the user if the password and username cookies are empty or false. But no matter what I do it always sends the user to "/wrong2". It doesnt even bother checking the if. This is what the code looks like at the moment:

        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")

I'm very new to python and are trying to learn it and I just cant find where the fault is. Can anyone see where it is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤云独去闲 2024-10-13 03:02:05

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

You're using hasattr to check to see if a dict contains a particular key, but you should be using the in operator instead. The hasattr function just checks to see if an object has a particular attribute.

So, you could instead write:

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

But I think a slightly better approach would be this, which ensures that empty usernames or passwords (think username = '') don't get let in:

# 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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文