Python Forum
Problem applying conditions against results back from a database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem applying conditions against results back from a database
#31
You cant check the result from the database before getting the result from the database.
You don't want to set usernamedata to None either that was just an example.
put it in the start of your main if statement
@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 not usernamedata:
        print('invalid username')
    eiif 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")
Reply
#32
Got it! Thanks, man! I had to add the return render_template line too but now it works. Appreciate the help now I get what I was doing wrong.

@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 not usernamedata:
        print('invalid username')
        return render_template("login.html")
    elif 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")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  applying total in a loop to a list JustinxFFx 1 2,188 Feb-11-2018, 03:14 AM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020