Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django static files are not updated

In my Django project the application my_app has a template which references a javascript static file:

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

Once I installed my_script.js in my_app/templates/my_app, everything seemed to work. At some point I overwrote my_script.js with a different script, such that my_script.js has a different content now.

However, when I load my_app with my browser, it loads the old my_script.js, although it does not exist anymore. How can I resolve it? Thanks.

like image 590
jazzblue Avatar asked Jul 21 '13 22:07

jazzblue


People also ask

How does Django manage static files?

Configuring static filesMake sure that django.contrib.staticfiles is included in your INSTALLED_APPS . In your templates, use the static template tag to build the URL for the given relative path using the configured STATICFILES_STORAGE . Store your static files in a folder called static in your app.

Where are Django static files stored?

Files are searched by using the enabled finders . The default is to look in all locations defined in STATICFILES_DIRS and in the 'static' directory of apps specified by the INSTALLED_APPS setting.

Does Django serve static files?

Django also provides a mechanism for collecting static files into one place so that they can be served easily. 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 .


1 Answers

I often use ?v=00001 or any define number to force clear cache in browser. So in your case it could be:

<script src="{% static 'my_app/my_script.js?v=00001' %}"></script>

Or:

<script src="{% static 'my_app/my_script.js' %}?v=00001"></script>

Next time you change the script, increase the number to 00002. Of course there are many ways to do this but I still prefer this method.

like image 152
Hieu Nguyen Avatar answered Oct 18 '22 12:10

Hieu Nguyen