Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python A Social Network with Flask Making Strong Users Bcryptkeeper

Andy Hughes
Andy Hughes
8,478 Points

validate password - can't seem to figure this out. [Solved]

Ok, I'm beat! I've tried to complete the second part of this code challenge and nothing works. I've got it to where it seems to just tell me that the passwords don't match. Obviously it is looking for them to return True, but I'm stuck on how.

I've tried an "if ==" block but that didn't work and I've tried various combinations of parameter to the "check_password_hash". I've also watched the video back at least 3 times and can't find where this bit is.

Help at this point would be greatly appreciated.

password_hashing.py
from flask.ext.bcrypt import generate_password_hash, check_password_hash

def set_password(user, password):
    user.password = generate_password_hash(password)
    return user

def validate_password(user, password):
        check_password_hash(user.password, password)
Andy Hughes
Andy Hughes
8,478 Points

Ok I figured it out. Just to say, the video is not a very good indicator of what is required for this challenge. It didn't cover it in the same way at all. My mind was thinking "Kenneth did one line of code to check, so why can't I?". Maybe you can do it with one line but that's not how I ended up doing it.

In the end I did use an if block and just returned True or False if the condition was met or not. So the code ended up looking as follows:

from flask.ext.bcrypt import generate_password_hash, check_password_hash

def set_password(user, password):
    user.password = generate_password_hash(password)
    return user

def validate_password(user, password):
    if check_password_hash(user.password, password):
        return True
    else:
        return False
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Hey Andy Hughes, kudos for figuring it out!

I’ve retitled post as Solved.

One small feedback point about a more pythonic why to write your last if statement.

# instead of
    if check_password_hash(user.password, password):
        return True
    else:
        return False

# directly use the truthiness of the result
    return check_password_hash(user.password, password)

Happy coding!!

1 Answer

Andy Hughes
Andy Hughes
8,478 Points

Chris Freeman - Thank you Chris. Great piece of feedback. Still trying to get my head around shortening code.