Django won't refresh staticfiles

Solution 1:

  • Clearing static file python manage.py collectstatic --noinput --clear. This will clear the statics beforehand.

  • Clear the browser cache

  • Add a random string after the js file include, e.g jquery.js?rand=23423423, with each load.

Solution 2:

It sounds like both your browsers have the javascript file cached. In Chrome you can clear the cache by pressing Ctrl + Shift + Del and ticking just 'Cached images and files'. Firefox probably has a similar shortcut.

You can take a look at this question on tips to disable caching of static files on your development server altogether.

Solution 3:

You need to bust the browser cache. This template tag will output a time based uuid when DEBUG=True. Otherwise it will look for a PROJECT_VERSION environment variable. If that is not found it will output a static version number.

import os
import uuid
from django import template                                                                                                              
from django.conf import settings                                                                                                         

register = template.Library()                                                                                                            

@register.simple_tag(name='cache_bust')                                                                                                  
def cache_bust():                                                                                                                        

    if settings.DEBUG:                                                                                                                   
        version = uuid.uuid1()                                                                                                           
    else:                                                                                                                                
        version = os.environ.get('PROJECT_VERSION')                                                                                       
        if version is None:                                                                                                              
            version = '1'                                                                                                                

    return '__v__={version}'.format(version=version)

You would use in a template like this:

{% load cache_bust %}

<link rel="stylesheet" href="{% static "css/project.css" %}?{% cache_bust %}"/>

and here is the resulting output:

<link rel="stylesheet" href="/static/css/project.css?__v__=7d88de4e-7258-11e7-95a7-0242ac130005"/>

Solution 4:

Instead of using complicated solutions you can add extra parameter to your includes in the templates.

For static includes:

<script src="{% static 'js/polls/polls.js' %}?version=1"></script>

For direct includes:

<link rel="stylesheet" type="text/css" href="/site_media/css/style.css?version=1" />  

Note the ?version=1 in the code. Every time you're modifying the css/js file, change this version in the template, so browser will be forced to reload the file.

And if you want to avoid caching at all for some unknown reason, you may use the current timestamp instead of version:

<link rel="stylesheet" type="text/css" href="/site_media/css/style.css?{% now "U" %}" />

Solution 5:

"Changes of staticfiles is not working". There can be multiple reasons for that.

  1. Your browser is storing cache of your static files.

    Solution (1): Open Incognito/Private window

    Solution (2): Use hard refresh:

    • For mac: cmd + shift + r
    • For others: ctr + shift + r
  2. If you're facing this issue in production then follow the steps below:

    • Remove staticfiles folder by following command: sudo rm -rf staticfiles [inside your project directory]

    • Now run collectstatic command: python3 manage.py collectstatic

    • Restart gunicorn, by following command: sudo systemctl restart gunicorn

    • Restart nginx, by following command: sudo systemctl restart nginx

Sometimes browser stores thes staticfiles data (as caches) for a certain time. After couple of hours the problem may gone.

Hope this will fix you issue.