Disable cache on a specific page using Flask

后端 未结 2 756
一整个雨季
一整个雨季 2020-12-19 04:54

I have a template showing various entries that the author can edit/delete. Users can delete their posts clicking on Delete

After the deletion the user gets redirecte

2条回答
  •  -上瘾入骨i
    2020-12-19 05:15

    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():
            ......
            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
    

提交回复
热议问题