@
fatea flask-login 不是自带登陆检测装饰器吗?
from flask_login import login_required
以下是他的代码:
''' python
def login_required(func):
'''
If you decorate a view with this, it will ensure that the current user is
logged in and authenticated before calling the actual view. (If they are
not, it calls the :attr:`LoginManager.unauthorized` callback.) For
example::
@
app.route('/post')
@
login_required def post():
pass
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()
...which is essentially the code that this function adds to your views.
It can be convenient to globally turn off authentication when unit testing.
To enable this, if the application configuration variable `LOGIN_DISABLED`
is set to `True`, this decorator will be ignored.
:param func: The view function to decorate.
:type func: function
'''
@
wraps(func)
def decorated_view(*args, **kwargs):
if current_app.login_manager._login_disabled:
return func(*args, **kwargs)
elif not current_user.is_authenticated:
return current_app.login_manager.unauthorized()
return func(*args, **kwargs)
return decorated_view
'''
BTW , is_authenticated 只是你的用户 ORM 模型里面的一个函数,用来验证是否用户通过验证罢了,并不是 boolean
` current_user = LocalProxy(lambda: _get_user()) `
根据你的设置,只想到你的用户 ORM 模型
所以 ` current_user.is_authenticated ` 就应该是自己设置的。 当然如果你不需要用到这个字段,设置成 boolean 或者 True 也是可以得。
未登录的使用 可以用
`if not current_user.is_authenticated(): pass `