我正在開發一個 Flask 應用程式,我想添加一個使用變數控制的自定義維護模式頁面。除了創建 if-then 陳述句檢查變數是否為真之外,我還有什么方法可以做到這一點:例如我會做什么:
@app.route("/mypage")
def mypage():
if (maintenance_mode == 1):
return render_template("maintenance.html")
return "response"
我想在不使用 if-then 陳述句的情況下執行此操作,最好只使用 1 @app.route。
uj5u.com熱心網友回復:
為此,您甚至可能不需要燒瓶并且可以在其他級別控制它,但是對于燒瓶,這里有一些很好的例子。
建議添加@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")
uj5u.com熱心網友回復:
If-then 陳述句適用于此目的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/376789.html
下一篇:VisualPython中的沖突
