Hi everybody,

I'm doing some programming in Flask, and as part of the program I'm doing a basic system of logging in, but that's not where my problem resides. During my programming, the page that is supposed to verify a login absolutely refuses to go past a specific line of code, regardless of what's past that line or what that line is doing. Here's the code:

@app.route('/loginConfirm')
def loginConfirm(): 
    try:

        username = request.args.get('usernameField')
        password = request.args.get('passwordField')

        for user in usersList:
            if(username == user.username):
                print(user.hashedPassword == sha256(password.encode('utf-8')).hexdigest())
                if(user.hashedPassword == sha256(password.encode('utf-8')).hexdigest()):
                    session.pop('uuid')
                    session['uuid'] = user.uuid
                    return redirect('/')

        return redirect('/login')

    except:

        return redirect('/login')

In this example, it will get to the print statement where I print whether or not the password is equivalent to the other (this returns True when its supposed to and is working correctly.). The problem lies in the fact that if I were to add another print statement directly after that, or any other type of statement including the if statement that is already there, then it won't execute, but it also won't throw an error. What confuses me is that the fact that it occurs only on this line makes me think it's something to do with an indentation error or a problem with the fact that its in a try statement, but if either of those are true then I can't find the result. Any help is greatly appreciated.

Recommended Answers

What happens if you comment out the print statement and replace it just with something simple like print('hello world')? What if you have two print statements in a row, as so:

print('hello')
print('world')

Do both of them execute?

Jump to Post

All 3 Replies

What happens if you comment out the print statement and replace it just with something simple like print('hello world')? What if you have two print statements in a row, as so:

print('hello')
print('world')

Do both of them execute?

commented: Despite changing literally nothing else, I did this and it started working, which is somewhat boggling to me. Anyway, the problems fixed. +1

You should never (ever) have an unqualified except. Doing so masks errors. Remove the try/except and let the error crash your code so you can see what the problem is.

commented: The reason the try/except is there is because it stops the page from crashing if there are no arguments present, as well as the session.pop() +1

The reason the try/except is there is because it stops the page from crashing if there are no arguments present, as well as the session.pop()

Then you should add an except clause for that specific exception. By having a generic exception handler you mask all other errors.

Let's try a real world example. You do your banking online but you don't want to get notified every time you do a transaction so you disable all notifications. Now you aren't getting emails for each time you use your debit card. But you also won't get a notification when someone gets access to your account and transfers out $50,000.

Incidentally, my account lets me specify "only notify me for transactions exceeding $x".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.