使用 Flask 在特定页面上禁用缓存
Disable cache on a specific page using Flask
我有一个显示作者可以 edit/delete 的各种条目的模板。
用户可以点击删除删除他们的帖子
删除后跳转到条目页面,但条目还在,需要重新加载页面才能显示删除效果。
如果我禁用缓存,问题就会消失,但我真的想在所有其他页面中都有缓存...
添加这些标签没有用,我想我的浏览器会忽略它们
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
我正在启用缓存槽:
@app.after_request
def add_header(response):
response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
response.headers['Cache-Control'] = 'public, max-age=600'
return response
有什么方法可以为特定页面禁用它吗?
编辑
按照建议,我尝试使用包装器:
def no_cache(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)
并在 @no_cache 装饰器中包装我想要的没有缓存的页面,仍然没有运气......
您可以尝试添加缓存控制 headers 只有当特定页面没有 headers 时:
@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
中的 headers - 仅适用于未明确关闭缓存的页面。
此外,您可能希望将添加 headers 的代码移动到 @no_cache
等包装器中 - 这样您就可以像这样使用它:
@app.route('/page_without_cache')
@no_cache
def page_without_cache():
return 'hello'
虽然 NikitaBaksalyar 的回答指向了正确的方向。我很难让它工作。页面代码给我一个错误 missing response
.
解决方法很简单。使用 make_response 方法。
from flask import make_response
对于每页缓存控制设置:
@app.route('/profile/details', methods=['GET', 'POST'])
def profile_details():
...<page specific logic>...
response = make_response(render_template('profile-details.html'))
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
response.headers['Pragma'] = 'no-cache'
return response
默认缓存控制设置:
@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
我有一个显示作者可以 edit/delete 的各种条目的模板。 用户可以点击删除删除他们的帖子
删除后跳转到条目页面,但条目还在,需要重新加载页面才能显示删除效果。 如果我禁用缓存,问题就会消失,但我真的想在所有其他页面中都有缓存...
添加这些标签没有用,我想我的浏览器会忽略它们
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
我正在启用缓存槽:
@app.after_request
def add_header(response):
response.headers['X-UA-Compatible'] = 'IE=Edge,chrome=1'
response.headers['Cache-Control'] = 'public, max-age=600'
return response
有什么方法可以为特定页面禁用它吗?
编辑
按照建议,我尝试使用包装器:
def no_cache(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)
并在 @no_cache 装饰器中包装我想要的没有缓存的页面,仍然没有运气......
您可以尝试添加缓存控制 headers 只有当特定页面没有 headers 时:
@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
中的 headers - 仅适用于未明确关闭缓存的页面。
此外,您可能希望将添加 headers 的代码移动到 @no_cache
等包装器中 - 这样您就可以像这样使用它:
@app.route('/page_without_cache')
@no_cache
def page_without_cache():
return 'hello'
虽然 NikitaBaksalyar 的回答指向了正确的方向。我很难让它工作。页面代码给我一个错误 missing response
.
解决方法很简单。使用 make_response 方法。
from flask import make_response
对于每页缓存控制设置:
@app.route('/profile/details', methods=['GET', 'POST'])
def profile_details():
...<page specific logic>...
response = make_response(render_template('profile-details.html'))
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
response.headers['Pragma'] = 'no-cache'
return response
默认缓存控制设置:
@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