Python Forum

Full Version: Problem applying conditions against results back from a database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4
Hey guys I'm trying to compare user input with what's in the database to see if they can login or not.

I'm getting a list back from the input and it's adding paranthesis and commas and apostrophes to each value so it's not matching up when compared.

How can I get rid of these extra special characters?

Thanks

@app.route("/login", methods=["GET","POST"])
def login():
    password = request.form.get("password")
    username = request.form.get("username")
    usernamedata = db.execute("SELECT username FROM users WHERE username=:username", {"username":username}).fetchone()
    passworddata = db.execute("SELECT password FROM users WHERE username=:username", {"username":username}).fetchone()
    print(usernamedata)
    print(passworddata)
    print(username)
    print(password)
    if username == usernamedata and password == passworddata:
        return render_template("login.html")
    else:
        return render_template("login.html")
@app.route("/login", methods=["GET","POST"])
def login():
    password = request.form.get("password")
    username = request.form.get("username")
    usernamedata = db.execute("SELECT username FROM users WHERE username=:username", {"username":username}).fetchone()
    passworddata = db.execute("SELECT password FROM users WHERE username=:username", {"username":username}).fetchone()
    username1 = usernamedata[0];
    password1 = passworddata[0];
    print(username1)
    print(password1)
    print(username)
    print(password)
    if username == usernamedata and password == passworddata:
        return render_template("login.html")
    else:
        return render_template("login.html")
I'm a genius and I figured it out!

never mind - now it doesn't work for some reason.

for some reason now i can't do usernamedata[0] since it's only one result i assume - but it did let me do it at first.

why is fetchone() returning all the extra junk characters!?
It would help to see the output, since we can't run your app.

A few asides:

1. Doing 2 queries is unnecessary.
2. The semi-colons on 7 and 8 are unnecessary.
I changed my code to this. So when I load it up with no values it returns "empty"

When I enter the correct values it works and moves me to "welcome"

However, when I type in a wrong username or password, I get an error saying this : https://pastebin.com/FkpuKB9d

@app.route("/login", methods=["GET","POST"])
def login():
    password = request.form.get("password")
    username = request.form.get("username")
    usernamedata = db.execute("SELECT username FROM users WHERE username=:username", {"username":username}).fetchone()
    passworddata = db.execute("SELECT password FROM users WHERE username=:username", {"username":username}).fetchone()
    
    if username is not None and usernamedata[0] == username and passworddata[0] == password:
        print("welcome")
        return render_template("welcome.html")
    elif username is not None and (usernamedata[0] != username or passworddata[0] != password):
        print("no match")
        return render_template("login.html")
    else:
        print("empty")
        return render_template("login.html")
By the way, when I type in the wrong username and the right password or vice versa, it does give me the "no match" and reloads login.html. So what am I missing? Thanks!
Where do you think the None is coming from? Work backwards from the relevant line in the traceback.

Also, None is falsy, so it's more Pythonic to write, e.g. if username.
i tried if username and it didn't seem to work.

What do you mean where the none is coming from? I'm not getting none anymore.
Your traceback suggests you're trying to subscript (i.e. index) a None. Are you saying that you've fixed that problem now?
oh no that is the issue I'm having.

I can't find where the none is coming from.
What variable has the None in it? Where is that variable assigned?
" if username is not None and usernamedata[0] == username and passworddata[0] == password:"
Pages: 1 2 3 4