Turn off caching of static files in Django development server

后端 未结 9 2188
灰色年华
灰色年华 2020-11-30 04:52

Is there an easy way to turn off caching of static files in Django\'s development server?

I\'m starting the server with the standard command:

<
相关标签:
9条回答
  • 2020-11-30 05:26

    This has nothing with Django, because nothing changed after I reinstall Django using pip.

    This is the behavior of browser, so you just need to clear cached images files of your browser.

    Ref

    Chrome Clear cache and cookies

    0 讨论(0)
  • 2020-11-30 05:28

    For newer Django, the way middleware classes are written has changed a bit.

    Follow all the instructions from @aaronstacy above, but for the middleware class, use this:

    class NoCache(object):
        def __init__(self, get_response):
            self.get_response = get_response
    
        def __call__(self, request):
            response = self.get_response(request)
            response['Cache-Control'] = 'must-revalidate, no-cache'
            return response
    
    0 讨论(0)
  • 2020-11-30 05:29

    Use whitenoise. There's a lot of issues with the static file serving in runserver and they're all already fixed in whitenoise. It's also WAY faster.

    They've talked about just replacing the built-in static serving with it, but no one has gotten around to it yet.

    Steps to use it in development...

    Install with pip install whitenoise

    Add the following to the end of settings.py:

    if DEBUG:
        MIDDLEWARE = [
            'whitenoise.middleware.WhiteNoiseMiddleware',
        ] + MIDDLEWARE
        INSTALLED_APPS = [
            'whitenoise.runserver_nostatic',
        ] + INSTALLED_APPS
    
    0 讨论(0)
提交回复
热议问题