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 trialShawndee Boyd
6,002 PointsFinally, create a function named load_user
It is just saying try again but I know that this is correct. Please help!
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
33,371 PointsShawndee, first thing I notice is the you are missing the "" surrounding name in the app=Flask(name__) code.
Also, a few typos or similar:
- The comparison has to be done with two == instead of just one.
- You are missing the ":" after except models.DoNotExist
- It should be: models.DoesNotExist instead of models.DoNotExist
- in the end, "none" should write "None"
I should then work
Vittorio
Vittorio Somaschini
33,371 PointsHello 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
6,002 PointsI 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
33,371 PointsHey.
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
6,002 PointsNow 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
6,002 PointsIt worked!!!! I got so frustrated that I started making syntax errors.