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

Shawndee Boyd
Shawndee Boyd
6,002 Points

Finally, create a function named load_user

It is just saying try again but I know that this is correct. Please help!

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

import models

app = Flask(__name__)
app.secret_key = 'ajklfaslkgalksgdha;lkhga;lk'

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

6 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Shawndee, first thing I notice is the you are missing the "" surrounding name in the app=Flask(name__) code.

Also, a few typos or similar:

  1. The comparison has to be done with two == instead of just one.
  2. You are missing the ":" after except models.DoNotExist
  3. It should be: models.DoesNotExist instead of models.DoNotExist
  4. in the end, "none" should write "None"

I should then work

Vittorio

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Shawndee.

You are actually pretty close to the right solution, but I see a couple of mistakes in the code you provided.

1) in the try statement you used a single = when you need 2 (==) for comparison. Please correct that.

2) your code does not return anything if the compiler finds a match: you will need to add a "return" in your try statement to actually return that user

Oops, also there is a third error, as "none" in your code (in the except statement) is not capitalized as it should be -> None

Let me know if all clear.

Vittorio

Shawndee Boyd
Shawndee Boyd
6,002 Points

I have done everything that you asked but now it is saying 'load_user' didn't return the correct user. Here is my code:

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

import models

app = Flask(name) app.secret_key = 'ajklfaslkgalksgdha;lkhga;lk'

login_manager = LoginManager() login_manager.init_app(app)

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

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hey.

I possibly did not explain it clearly; the return keyword needs to be put before the code you wrote in the try statement.

This is because the code you wrote (the very first one) was targeting the right user but was not returning it.

I meant that instead of

models.User.get(models.User.id = userid)

You would need to use:

return models.User.get(models.User.id = userid)

And not:

models.User.get(models.User.id == userid) return userid

Also, please note that you have removed the __ __ signs in the app = Flask(name) piece of code!

Vittorio

Shawndee Boyd
Shawndee Boyd
6,002 Points

Now it is saying "It looks like Task 1 is no longer passing". Ugh!!!

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

import models

app = Flask(name) app.secret_key = 'sdjfdsjfsjdgakjgfadsjf'

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.DoNotExist return none

Shawndee Boyd
Shawndee Boyd
6,002 Points

It worked!!!! I got so frustrated that I started making syntax errors.