Fighting client-side caching in Django

前端 未结 7 930
傲寒
傲寒 2020-11-27 13:18

I\'m using the render_to_response shortcut and don\'t want to craft a specific Response object to add additional headers to prevent client-side caching.

I\'d like to

7条回答
  •  情话喂你
    2020-11-27 13:58

    I was scratching my head when the three magic meta didn't work in Firefox and Safari.

    
    
    
    

    Apparently it can happen because some browsers will ignore the client side meta, so it should be handled at server side.

    I tried all the answers from this post for my class based views (django==1.11.6). But referring to answers from @Lorenzo and @Zags, I decided to write a middleware which I think is a simple one.

    So adding to other good answers,

    # middleware.py
    class DisableBrowserCacheMiddleware(object):
    
        def __init__(self, get_response):
            self.get_response = get_response
    
        def __call__(self, request):
            response = self.get_response(request)
            response['Pragma'] = 'no-cache'
            response['Cache-Control'] = 'no-cache, no-store, must-revalidate'
            response['Expires'] = '0'
            return response
    
    # settings.py
    MIDDLEWARE = [
        'myapp.middleware.DisableBrowserCacheMiddleware',
        ...
    

提交回复
热议问题