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!
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
Welby Obeng20,340 Points
Why do you compare models.User.id == userid?
#function to use if you need to look up a user @login_manager.user_loader def load_user(userid): try: # get user row with id return models.User.get(models.User.id == userid) # if does not exist return none except models.DoesNotExist: return None
Regarding the code above why do you compare models.User.id == userid in get function? why not return models.User.get(userid)? if userid doesn't exist it will throw models.DoesNotExist exception
regarding def load_user(userid), where in social network app do you use use this function and pass in user id? is it login_user(user) in login function?
Kenneth LoveTreehouse Guest Teacher
I'm gonna answer those backwards.
We don't use
flask-login does any time we need to check/fetch the current user. So, yes, when we login the user. When we do
@login_required. When we do
current_user. We have to provide that so
flask-login knows how to find our user accounts.
Why do the comparison? Mainly to keep it explicit and uniform through the rest of the code base. We do comparisons everywhere else and there's no harm in doing a comparison here.