有没有办法让 Flask 应用程序中的所有页面显示相同的内容?
Is there a way to make all pages in a Flask app show the same content?
我正在开发 Flask 应用程序,我想添加一个使用变量控制的自定义维护模式页面。除了创建 if-then 语句检查变量是否为真之外,还有什么方法可以做到这一点:例如我会做什么:
@app.route("/mypage")
def mypage():
if (maintenance_mode == 1):
return render_template("maintenance.html")
return "response"
我想在不使用 if-then 语句的情况下执行此操作,最好只使用 1 @app.route
.
你甚至可能不需要烧瓶来做这个并且可以在其他层面上控制它,但是烧瓶 here 是这样做的好例子。
建议添加@app.before_request
并查看维护标志。 (@app.before_request
会在所有请求之前调用,所以你不需要对所有50条路由进行维护检查)。
@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")
我正在开发 Flask 应用程序,我想添加一个使用变量控制的自定义维护模式页面。除了创建 if-then 语句检查变量是否为真之外,还有什么方法可以做到这一点:例如我会做什么:
@app.route("/mypage")
def mypage():
if (maintenance_mode == 1):
return render_template("maintenance.html")
return "response"
我想在不使用 if-then 语句的情况下执行此操作,最好只使用 1 @app.route
.
你甚至可能不需要烧瓶来做这个并且可以在其他层面上控制它,但是烧瓶 here 是这样做的好例子。
建议添加@app.before_request
并查看维护标志。 (@app.before_request
会在所有请求之前调用,所以你不需要对所有50条路由进行维护检查)。
@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")