flask的nocache防止js不刷新

原文:http://librelist.com/browser/flask/2011/8/8/add-no-cache-to-response/#952cc027cf22800312168250e59bade4

 

Method 1:

@app.route('/nocache')
def something_not_cached():
    resp = make_response(render_template(...))
    resp.cache_control.no_cache = True
    return resp

Or you write a decorator:

@app.route('/nocache')
@nocache
def something_not_cached():
    return render_template(...)

And here is the decorator:

from flask import make_response
from functools import update_wrapper

def nocache(f):
    def new_func(*args, **kwargs):
        resp = make_response(f(*args, **kwargs))
        resp.cache_control.no_cache = True
        return resp
    return update_wrapper(new_func, f)

resp.cache_control is an accessor for the Cache-Control header, you can
also modify the header directly:

resp.headers['Cache-Control'] = 'no-cache'

 

posted @ 2014-05-08 17:33  wangchao719  阅读(1415)  评论(0编辑  收藏  举报