@app.after_request
def add_header(response):
response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
if ('Cache-Control' not in response.headers):
response.headers['Cache-Control'] = 'public, max-age=600'
return response
@app.route('/page_without_cache')
def page_without_cache():
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
response.headers['Pragma'] = 'no-cache'
return 'hello'
重点是,你不应覆盖@app.after_request
所有页面的标题-仅适用于未明确关闭缓存的页面。
此外,你可能希望将添加标头的代码移动到诸如@no_cache
- 的包装器中,因此可以像这样使用它:
@app.route('/page_without_cache')
@no_cache
def page_without_cache():
return 'hello'