Chickenfresh
Chickenfresh

Reputation: 365

How to connect js script from html to Django static

I have new Django project with frontend, initially written not for Django at all, so I cannot connect this script:

<script>
    document.body.appendChild(document.createElement('script')).
    src='js/main_script.js?r='+Math.floor(Math.random()*99999999999999999999);
</script> 

The file js/main_script.js is located in static folder, other js scripts are loaded as intended. And as I understand this script prevents caching for some reason.

So, what am I missing in that part? Thanks in advance for any clarifications

Upvotes: 2

Views: 255

Answers (1)

Artem Bernatskyi
Artem Bernatskyi

Reputation: 4667

You should remove those +Math.floor(Math.random()*99999999999999999999) part and use proper solution for static files caching control

https://docs.djangoproject.com/en/2.1/ref/contrib/staticfiles/#manifeststaticfilesstorage

UPD in response to comments below

Suppose we have

STATIC_ROOT = '/var/web/chick_static/'
STATIC_URL = '/chick-static/'

Then when we use static template tag Django transforms
from

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

to

<script src="/chick-static/js/main_script.js"></script>

Also Django will be looking for file in /var/web/chick_static/ directory.

Upvotes: 2

Related Questions