Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python A Social Network with Flask Making Strong Users Bcryptkeeper

Andy Hughes
Andy Hughes
8,113 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,113 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 67,982 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,113 Points

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