I did this in my instruments project. I use the before_request
decorator:
@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)
and I then created an is_public()
decorator for the few places that would need to be accessible without login:
def public_endpoint(function):
function.is_public = True
return function
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…