2017-02-10 33 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