S
S
Sergey Eremin2015-04-12 04:41:05
Django
Sergey Eremin, 2015-04-12 04:41:05

How can I disable the strange /static/ cache protection behavior in Django?

Weird calls to JavaScript files from the /static/ folder in Django have been noticed. Basically, the files are called correctly. For example:

"GET /static/js/jquery-2.1.1.min.js HTTP/1.1" 304 0

But for some reason, some are protected from caching and are called like this:
"GET /static/js/login-logout.js?_=1428801732678 HTTP/1.1" 200 1377

All JS files called from the base template (including templates that inherit the base) are called normally. As expected, they contain something like:
{% load staticfiles %}
<script src="{% static 'js/jquery-2.1.1.min.js' %}" type="text/javascript"></script>

But for templates that do not inherit from the base one, for example, those called via:
<script type="text/javascript">
$(document).ready(function(){
  $('#login-logout').load('login-logout');
})
</script>

and containing an identical construction:
{% load staticfiles %}
<script src="{{ STATIC_URL }}js/login-logout.js" type="text/javascript"></script>

there is a substitution at the end of the call ?_=1428801732678 (the numbers are different each time). I understand that Django itself does this to prevent caching. How to turn it off?
PS So I understand that this is not in the settings.py settings . After all, there are single backend settings for /static/ and daddy js/ , which means that the calls must be identical and provide for the possibility of caching.
PPS The strangest thing is that if you write in the template:
<script src="/static/js/loginlogout.js" type="text/javascript"></script>
then all the same, this ?_=12bunch4digits5 is added to the call

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2015-04-12
@Sergei_Erjemin

Django doesn't do magic
To clarify, look at the code of the page that you got - it's probably not there _=12345
So django has nothing to do with it
And what does jq ajax with its anti- cache
https://api.jquery.com/load/
cache jq globally however not worth it

But for templates that do not inherit from the base one, for example
don't save on matches - make one js file and load it on the first run. If the file is less than 256kb, no one will notice it (unless you have a specific site)
An example of what to do if I didn’t convince you api.jquery.com/jQuery.getScript Better
yet, just use $.ajax({cache: true , dataType: 'script ' )
)
$(document).ready(function(){
  // $('#login-logout').load('login-logout');
  $.ajax(function(){
  cache: true,
  url: 'login-logout.html',
  dataType: 'html'
  success: function(html){
  $('#login-logout').html(html) }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question