Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there a way to make all pages in a Flask app show the same content?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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") 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading