Django won't refresh staticfiles

Alejandro Veintimilla picture Alejandro Veintimilla · Jan 12, 2015 · Viewed 78.9k times · Source

This is annoying. I have a javascript file referenced on a django template:

 <script src="{% static 'js/login.js' %} "></script>

I made some changes to that js file. Then, I refresh the page and I can't see the changes happen.

If I remove the javascript from the file and put it in the HTML, it works correctly, but if I try to do it using the external js file then it doesn't. I have tried closing the server and running runserver several times, I've also tried changing from Firefox to Chrome. It makes no sense at all. Please help me understand, I can get away with including all the js inside the HTML but it bothers me that I can't do it the normal way anymore.

More details about this error (the #$&%# weirdest thing I've found):

The javascript is something like this:

old_sentence = "Dig that hole, forget the sun"  // this is what I want to change
$("#jqselector").text(old_sentence)

new_sentence = "Run, rabbit, run"  // this is the new sentence, it just won't show up.

So, I changed the js and restart the server, still the html shows the old sentence. Then I deleted the reference to the login.js file and put all the js inside script tags in the HTML, of course, the new sentence appears. Then I include the login.js file, comment out the js inside the html BUT I delete all the content inside the login.js file, making it an empty file... but the old sentence still shows up. Therefore the old login.js file must be cashed somewhere I don't know. Then I open Chrome and try it all again, same problem.

What can it be? is there an option to force django to refresh staticfiles? I thought restarting the server was enough. .. should I reboot my computer? :/ lol.

Answer

Danial Tz picture Danial Tz · Jan 12, 2015
  • 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.

Did it help?