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
#26
OK well I am entering the data. I have a registration code here :

@app.route("/register", methods=["GET", "POST"])
def register():
    
    if request.method == "GET":
        return render_template("register.html")
    elif request.method == "POST":
        password = request.form.get("password")
        username = request.form.get("username")
        session.clear()
        db.execute("INSERT INTO users (username, password) VALUES (:username, :password)", {"username":username, "password":password})
        db.commit()
        return render_template("login.html")
the issue with my app is:

1) When I login with the incorrect username and password - i get an error.
2) When I login with a correct username and incorrect password - it works.
3) When I login with correct username and correct password - it works.

It only doesn't work on the first option when both are wrong - it gives me an error like this :

Error:
TypeError TypeError: 'NoneType' object is not subscriptable Traceback (most recent call last) File "C:\Python38\Lib\site-packages\flask\app.py", line 2464, in __call__ return self.wsgi_app(environ, start_response) File "C:\Python38\Lib\site-packages\flask\app.py", line 2450, in wsgi_app response = self.handle_exception(e) File "C:\Python38\Lib\site-packages\flask\app.py", line 1867, in handle_exception reraise(exc_type, exc_value, tb) File "C:\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise raise value File "C:\Python38\Lib\site-packages\flask\app.py", line 2447, in wsgi_app response = self.full_dispatch_request() File "C:\Python38\Lib\site-packages\flask\app.py", line 1952, in full_dispatch_request rv = self.handle_user_exception(e) File "C:\Python38\Lib\site-packages\flask\app.py", line 1821, in handle_user_exception reraise(exc_type, exc_value, tb) File "C:\Python38\Lib\site-packages\flask\_compat.py", line 39, in reraise raise value File "C:\Python38\Lib\site-packages\flask\app.py", line 1950, in full_dispatch_request rv = self.dispatch_request() File "C:\Python38\Lib\site-packages\flask\app.py", line 1936, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "C:\school\project1\application.py", line 42, in login if username is not None and usernamedata[0] == username and passworddata[0] == password: TypeError: 'NoneType' object is not subscriptable
Here's my code in question:

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


Messages In This Thread
RE: Problem comparing two values - by card51shor - Jun-12-2020, 02:59 AM
RE: Problem comparing two values - by ndc85430 - Jun-12-2020, 03:48 AM
RE: Problem comparing two values - by card51shor - Jun-12-2020, 04:01 AM
RE: Problem comparing two values - by ndc85430 - Jun-12-2020, 04:09 AM
RE: Problem comparing two values - by card51shor - Jun-12-2020, 05:03 AM
RE: Problem comparing two values - by ndc85430 - Jun-12-2020, 05:05 AM
RE: Problem comparing two values - by card51shor - Jun-12-2020, 05:07 AM
RE: Problem comparing two values - by ndc85430 - Jun-12-2020, 05:09 AM
RE: Problem comparing two values - by card51shor - Jun-12-2020, 05:10 AM
RE: Problem comparing two values - by ndc85430 - Jun-12-2020, 05:12 AM
RE: Problem comparing two values - by card51shor - Jun-12-2020, 05:14 AM
RE: Problem comparing two values - by ndc85430 - Jun-12-2020, 05:17 AM
RE: Problem comparing two values - by card51shor - Jun-12-2020, 05:24 AM
RE: Problem comparing two values - by ndc85430 - Jun-12-2020, 05:27 AM
RE: Problem comparing two values - by card51shor - Jun-12-2020, 05:29 AM
RE: Problem comparing two values - by ndc85430 - Jun-12-2020, 05:33 AM
RE: Problem comparing two values - by card51shor - Jun-12-2020, 05:35 AM
RE: Problem comparing two values - by ndc85430 - Jun-12-2020, 05:41 AM
RE: Problem comparing two values - by card51shor - Jun-12-2020, 06:04 AM
RE: Problem comparing two values - by card51shor - Jun-12-2020, 11:54 PM
RE: Problem comparing two values - by pyzyx3qwerty - Jun-13-2020, 05:12 AM
RE: Problem comparing two values - by Yoriz - Jun-13-2020, 08:51 AM
RE: Problem comparing two values - by card51shor - Jun-13-2020, 05:21 AM
RE: Problem comparing two values - by pyzyx3qwerty - Jun-13-2020, 07:56 AM
RE: Problem comparing two values - by card51shor - Jun-13-2020, 04:36 PM
RE: Problem comparing two values - by Yoriz - Jun-13-2020, 05:00 PM
RE: Problem comparing two values - by card51shor - Jun-13-2020, 05:13 PM
RE: Problem comparing two values - by Yoriz - Jun-13-2020, 05:19 PM
RE: Problem comparing two values - by card51shor - Jun-13-2020, 05:22 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  applying total in a loop to a list JustinxFFx 1 2,275 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