[Answered ]-Django – Javascript – Disable cache on image refresh

2πŸ‘

βœ…

As @yedpodtrzitko pointed out, adjusting the cache headers of the web page in Django is pointless.

This part looks wrong:

$.get('{{ MEDIA_URL }}{{ data_to_modify.thumbnail.name }}?cachebuster='+Math.floor(Math.random()*100 +1), function(data) {

{{ MEDIA_URL }}{{ data_to_modify.thumbnail.name }} is the url of of the image you want to display right? (you use the same url in the html img tag)

In that case it doesn’t make sense to load that url via ajax.

It seems like you don’t need ajax at all, you just need to periodically update the src attribute of the img tag, with the cachebuster querystring appended:

<script type='text/javascript'>
$(document).ready(function() {
    var $img = $('#imageactual');
    setInterval(function() {
        $img.attr('src', '{{ MEDIA_URL }}{{ data_to_modify.thumbnail.name }}?cachebuster='+Math.floor(Math.random()*100 +1));
    }, 2000);
});
</script>
πŸ‘€Anentropic

Leave a comment