2

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

3
  • What is the purpose of Math.random()*99999999999999999999? Commented Nov 10, 2018 at 14:17
  • @roganjosh i think it is hand made version of docs.djangoproject.com/en/2.1/ref/contrib/staticfiles/… Commented Nov 10, 2018 at 14:24
  • @roganjosh, I've found it on stackoverflow, apparently used to prevent caching though i'm not an author Commented Nov 10, 2018 at 23:57

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

but, at least for understanding, why without static-way of loading this script it works while opening .html file, but at Django doesn’t?
@Chickenfresh i've updated answer, plz check if it is understandable, if not then feel free to ask for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.