我正在尝试创建一个检查用户是否已登录的函数。我已将该函数放在mainpage类之外,并且在我尝试使用它之前它没有出现任何错误MainPage类。该函数如下所示:
def LoginCheck():
username = self.request.cookies.get('username')
password = self.request.cookies.get('password')
if username and password:
checkq = db.GqlQuery("SELECT * FROM Users WHERE username = :1 AND password = :2", username, password)
checkresult = checkq.get()
if checkresult is None:
self.redirect("/wrong")
else:
self.redirect("/wrong2")
当我尝试使用它时会返回:
line 14, in LoginCheck
username = self.request.cookies.get('username')
NameError: global name 'self' is not defined
我做错了什么?
答案 0 :(得分:5)
您必须在函数定义中添加“self”。请参阅section 9.3.2 of python's tutorial。
def LoginCheck(self):
username = self.request.cookies.get('username')
password = self.request.cookies.get('password')
if username and password:
checkq = db.GqlQuery("SELECT * FROM Users WHERE username = :1 AND password = :2", username, password)
checkresult = checkq.get()
if checkresult is None:
self.redirect("/wrong")
else:
self.redirect("/wrong2")