Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django won't refresh staticfiles

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?

like image 767
Alejandro Veintimilla Avatar asked Jan 12 '15 21:01

Alejandro Veintimilla


People also ask

How do I run Collectstatic in Django?

Using the collectstatic command, Django looks for all static files in your apps and collects them wherever you told it to, i.e. the STATIC_ROOT . In our case, we are telling Django that when we run python manage.py collectstatic , gather all static files into a folder called staticfiles in our project root directory.


2 Answers

  • 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.

like image 191
Danial Tz Avatar answered Sep 20 '22 12:09

Danial Tz


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.

like image 33
knbk Avatar answered Sep 23 '22 12:09

knbk