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 Build a Social Network with Flask Takin' Names Set up LoginManager

Finally, create a function named load_user that takes a user's id attribute as an argument.

Here is the question on the 'social network' track that I am stuck on

Finally, create a function named load_user that takes a user's id attribute as an argument. Inside the function, look up a models.User instance with the id and return it. Return None if the User doesn't exist. Decorate the function with @login_manager.user_loader.

I've looked this thing over and over and am having a hard time determining where I went astray! HELP!!!

lunch.py
from flask import Flask, g
from flask.ext.login import LoginManager

import models

app = Flask(__name__)
app.secret_key = 'asln23098hAgf4894ajkn..239nlna901!asdfeie9'

login_manager = LoginManager()
login_manager.init_app(app)

@login_manager.user_loader
def load_user(userid):
  try:
    models.User.get(models.User.id == userid)
  except models.DoesNotExist:
    return None

2 Answers

Lukas Smith
Lukas Smith
4,026 Points

I don't know python but you have in instruction "with the id and return it. " so maybe should like return models.User.get(models.User.id == userid) Now you only return if is none

I think you are correct. Thanks!

from flask import Flask, g from flask.ext.login import LoginManager

import models

app = Flask(name) app.secret_key = 'asln23098hAgf4894ajkn..239nlna901!asdfeie9'

login_manager = LoginManager() login_manager.init_app(app)

@login_manager.user_loader def load_user(userid): try: return models.User.get(models.User.id == userid) except models.DoesNotExist: return None