2017-02-10 43 views
0

Flask中有任何'全局GET管理'的方法嗎?Flask中的全局GET管理

例如: 我想在我的燒瓶應用程序的任何頁面上通過彈出窗口顯示錯誤消息。如果用戶點擊'關閉'按鈕,應用程序將使用新的get參數'message_read = 1'重新加載頁面。 我想抓住這個GET參數。我確信有一個更好的方法,然後在每一個app.route(這是很多)寫支票。你能給我一個提示嗎?

謝謝。

+0

查看答案並接受或評論 –

回答

0

您可以使用decrators。閱讀關於蟒蛇裝飾here

這裏是低於flask.The代碼定製的裝飾的演示顯示了您的使用情況下,裝飾定義和使用

代碼

from flask import Flask,request 
from functools import wraps 


def popup_message(f): 
    @wraps(f) 
    def f_(*args,**argv): 
     message_read = request.args.get('message_read', None) 
     if message_read is not None: 
      return message_read 
     else: 
      return f(*args,**argv) 
    return f_ 




app = Flask(__name__) 

@app.route('/earth') 
@popup_message 
def hello_earth(): 
    return 'Hello,earth' 

@app.route('/world') 
@popup_message 
def hello_world(): 
    return 'Hello, World!' 

app.run() 

使用

運行應用程序爲

python main.py 
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 

並嘗試向/ earth發送請求和/不管有沒有message_read