Reputation: 2263
I have a little signup page that I'm trying to get working. Basically you give your username, password, and email, and then if the username that you entered doesn't already exist then it redirects you to a welcome page that says, "welcome (username here)!". If the user DOES exist, then it re-renders the signup page. The problem is, even if the user doesn't exist, it keeps re-rendering the signup page. I get no errors. The function "getByName" that checks if the user exists keeps returning true. Can anybody see the issue?
here's the code that calls the "getByName" function.
if(username and password and verify and email):
if UserInfo.getByName(username) == True:
self.renderSignup()
else:
UserInfo.register(username, password, email)
cookieVal = str(encrypt.makeCookieHash(username))
self.response.headers.add_header("Set-Cookie", "username=%s; Path=/" % (cookieVal))
self.redirect("/welcome")
else:
self.renderSignup(username, email, usernameError, passwordError, verifyError, emailError)
Here's the "UserInfo" data model that contains the "getByName" function, along with others.
class UserInfo(db.Model):
passwordHash = db.StringProperty(required = True)
email = db.StringProperty(required = True)
username = db.StringProperty(required = True)
@classmethod
def register(cls, name, password, email):
encrypt = Encrypt()
pwHash = encrypt.hashUserInfo(name, password)
UserInfo(passwordHash = pwHash, email = email, username = name).put()
@classmethod
def getByName(cls,name):
usersWithName = UserInfo.gql("where username = :1", name)
if usersWithName != None:
return True
return False
Upvotes: 0
Views: 586
Reputation: 12986
The lines
usersWithName = UserInfo.gql("where username = :1", name)
if usersWithName != None:
where you do the comparison is where you are going wrong. userWithName
is a query object and your comparison will alwys be true. You need to execute the query with a get, fetch etc.... and then see if the result set has contents.
You should be doing something like
usersWithName = UserInfo.gql("where username = :1", name).get()
if usersWithName != None:
Or you could do a fetch(10), and check for a non empty list. Of course if you found more than 1 item matching, then that would indicate a broader error in your system.
Upvotes: 2