Вы используете 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")