Python 使用Flask禁用特定页面上的缓存

Python 使用Flask禁用特定页面上的缓存,python,caching,flask,Python,Caching,Flask,我有一个模板,显示了作者可以编辑/删除的各种条目。 用户可以单击“删除”删除其帖子 删除后,用户被重定向到entries页面,但该项目仍然存在,需要重新加载页面以显示删除效果。 如果我禁用缓存,问题就会消失,但我真的希望在所有其他页面中都有缓存 添加这些标记不起作用,我想我的浏览器只是忽略了它们 <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" /> <meta http

我有一个模板,显示了作者可以编辑/删除的各种条目。 用户可以单击“删除”删除其帖子

删除后,用户被重定向到entries页面,但该项目仍然存在,需要重新加载页面以显示删除效果。 如果我禁用缓存,问题就会消失,但我真的希望在所有其他页面中都有缓存

添加这些标记不起作用,我想我的浏览器只是忽略了它们

<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" />
有没有一种方法可以为特定页面禁用它

编辑

根据建议,我尝试使用包装器:

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\u缓存装饰器中,但仍然没有运气…

只有在特定页面没有此类标题时,才可以尝试添加缓存控制标题:

@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\u request
中覆盖所有页面的标题-仅适用于缓存未明确关闭的页面

此外,您可能希望将添加头的代码移动到包装器中,例如
@no_cache
——这样您就可以像这样使用它:

 @app.route('/page_without_cache')
 @no_cache
 def page_without_cache():
   return 'hello'

而NikitaBaksalyar的答案指向了正确的方向。我很难让它工作。页面代码给了我一个错误,
缺少响应

解决办法很简单。使用该方法

对于每页缓存控制设置:

    @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

感谢您指出,如果缓存控制头已经存在,我就无法添加它们,我没有想到。。。我试着按照你的建议去做,试着使用包装器,试着只添加响应头,但是缓存仍然存在,因为某些原因…@LucaBrozzi,请检查你是否在Chrome/Mozilla开发工具中获得了你想要的头。如果您不知道如何操作,请参考此答案:。如果您清楚地看到没有
缓存控制:没有缓存
标题,则您的
@应用程序中重写的标题仍然存在问题。在请求
处理程序之后。不,我忘记更改行,您的解决方案是正确的,非常感谢
    @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