Reputation: 257
This is most probably a stupid question, but still I can't find an answer.
Documentation for Flask-Security says:
If there are only certain times you need to require that your user is logged in, you can do so with:
if not current_user.is_authenticated:
return current_app.login_manager.unauthorized()
But it gives no clue where this "current_user" variable shall come from. Not surprisingly I get a "NameError: global name 'current_user' is not defined" when I try to use it in my application source code.
Weird thing is that, when I use the variable in my HTML template, render_template function somehow completes without an error.
So, the question is, how can I use "current_user" variable in my application and how does the template engine "know" about it if my app doesn't?
Upvotes: 4
Views: 4035
Reputation: 4862
You need to import current_user.
from flask_login import current_user
As far as where it comes from - it comes from a different package, flask-login:
#: A proxy for the current user. If no user is logged in, this will be an
#: anonymous user
current_user = LocalProxy(lambda: _get_user())
Upvotes: 4