I’m working on a Flask app and I wanted to add a custom maintenance mode page controlled using a variable. Is there a way I can do this besides creating if-then statements checking if the variable is true: E.g. what I would do:
@app.route("/mypage")
def mypage():
if (maintenance_mode == 1):
return render_template("maintenance.html")
return "response"
I’d like to do this without using an if-then statement and preferably just use 1 @app.route.
>Solution :
You might not even need flask for this and can control it on other levels, but for flask here are good examples to do so.
It is suggested to add @app.before_request and check maintenance flag. (@app.before_request will be called before all request, so you do not need to do maintenance check for all 50 routes).
@app.before_request
def check_under_maintenance():
if maintenance_mode == 1: #this flag can be anything, read from file,db or anything
abort(503)
@app.route('/')
def index():
return "This is Admiral Ackbar, over"
@app.errorhandler(503)
def error_503(error):
return render_template("maintenance.html")