喜欢这个问题:Best way to make Django's login_required the default
我正在使用 Flask-Login
的 login_required现在装修。有没有让它成为 Flask
中的默认行为?
最佳答案
我在我的 instruments 中这样做了项目。我使用 before_request
装饰器:
@app.before_request
def check_valid_login():
login_valid = 'user' in session # or whatever you use to check valid login
if (request.endpoint and
'static' not in request.endpoint and
not login_valid and
not getattr(app.view_functions[request.endpoint], 'is_public', False) ) :
return render_template('login.html', next=request.endpoint)
然后我为几个无需登录即可访问的地方创建了一个 is_public()
装饰器:
def public_endpoint(function):
function.is_public = True
return function
关于python - 使 Flask-Login 的 login_required 成为默认值的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13428708/