Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Not Reflecting Updates to Javascript Files?

Tags:

python

django

I have javascript files in my static folder. Django finds and loads them perfectly fine, so I don't think there is anything wrong with my configuration of the static options. However, sometimes when I make a change to a .js file and save it, the Django template that uses it does NOT reflect those changes -- inspecting the javascript with the browser reveals the javascript BEFORE the last save. Restarting the server does nothing, though restarting my computer has sometimes solved the issue. I do not have any code that explicitly deals with caching. Has anyone ever experienced anything like this?

like image 456
user1427661 Avatar asked Mar 26 '13 15:03

user1427661


People also ask

Can JavaScript and Django work together?

Talking about the future web apps, Django has a lot to offer and has the ability to accommodate any modern web apps structures. Stacking Django and Javascript web frameworks to build modern web apps is one of the best way to stack backend and frontend frameworks.

How integrate JavaScript with Django?

path/to/your/file. js is a path relative to the static folder. You should use django-compressor to minify your files on production environment. As long as the routes you defined in the Django app are the same that in the Java app for the AJAX calls, you shouldn't have to change the content of your javascript file.


3 Answers

I believe your browser is caching your js

you could power refresh your browser, or clear browser cache?

on chrome control+f5 or shift + f5

i believe on firefox it is control + shift + r

like image 166
dm03514 Avatar answered Sep 29 '22 10:09

dm03514


Since you are editing JavaScript files and watching for the changes in the browser I assume you are actively developing your Django app and probably using Django's development runserver. There is a better solution than clearing the browser cache and refreshing. If you run a watcher utility that supports the livereload protocol then your browser will automatically refresh whenever you change any static file.

The django-livereload-server python package provides a spiffy solution. Install it:

$ pip install django-livereload-server

Add 'livereload.middleware.LiveReloadScript' to MIDDLEWARE_CLASSES in settings.py.

Then run

$ ./manage.py livereload

before starting the runserver.

More documentation can be found at the django-livereload-server github site

like image 40
nmgeek Avatar answered Sep 29 '22 10:09

nmgeek


For me, opening Incognito Mode in Chrome let the browser show the recent changes in my .js static files.

like image 35
Sayyor Y Avatar answered Sep 29 '22 12:09

Sayyor Y