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:
<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.
Chrome Clear cache and cookies
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
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