开发者

Using selfmade functions in Google App Engine

开发者 https://www.devze.com 2023-01-28 16:20 出处:网络
I\'m trying to make a function that checks if the user is logged in. I\'ve placed开发者_Python百科 the function outside of the mainpage class and it gives no errors until I try to use it insie the def

I'm trying to make a function that checks if the user is logged in. I've placed开发者_Python百科 the function outside of the mainpage class and it gives no errors until I try to use it insie the def get(self) within the MainPage class. The function looks like this:

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

and When I try to use it it returns:

line 14, in LoginCheck
    username = self.request.cookies.get('username')
NameError: global name 'self' is not defined

What am I doing wrong?


You'll have to add "self" to your function definition. See 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")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号