Disable cache on a specific page using Flask

You can try adding cache control headers only if there are no such headers for a specific page:

@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

And in your page code - e.g.:

@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'

The point is that you shouldn't override your headers in @app.after_request for all pages - only for those where cache isn't turned off explicitly.

Further, you might want to move the code adding headers to a wrapper such as @no_cache - so you can use it just like that:

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

While the answer of NikitaBaksalyar is pointing into the right direction. I had trouble getting it to work. The page code gave me an error with missing response.

The solution is quite simple. Use the make_response method.

from flask import make_response

For per page cache-control settings:

    @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

Default cache-control settings:

    @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