Google App Engine 重定向问题
我正在尝试制作一个用户验证脚本,如果密码和用户名 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在使用
hasattr
来检查dict
是否包含特定键,但您应该使用in
运算符。hasattr
函数只是检查对象是否具有特定属性。所以,你可以写:
但我认为一个稍微更好的方法是这样,它确保空的用户名或密码(想想
username = ''
)不会被允许进入:You're using
hasattr
to check to see if adict
contains a particular key, but you should be using thein
operator instead. Thehasattr
function just checks to see if an object has a particular attribute.So, you could instead write:
But I think a slightly better approach would be this, which ensures that empty usernames or passwords (think
username = ''
) don't get let in: