简体   繁体   English

Django-Javascript-禁用图像刷新缓存

[英]Django - Javascript - Disable cache on image refresh

I use, in my template, the following javascript to refresh an image: 我在模板中使用以下JavaScript刷新图像:

<script type='text/javascript'>
$(document).ready(function() {
    var $img = $('#imageactual');
    setInterval(function() {
        $.get('{{ MEDIA_URL }}{{ data_to_modify.thumbnail.name }}?cachebuster='+Math.floor(Math.random()*100 +1), function(data) {
            var $loader = $(document.createElement('img'));
            $loader.one('load', function() {
                $img.attr('src', $loader.attr('src'));
            });
            $loader.attr('src', data);
            if($loader.complete) {
                $loader.trigger('load');
            }
        });
    }, 2000);
});
</script>

And I display the image with the following html code: 我用以下html代码显示图像:

<img  id="imageactual" src="{{ MEDIA_URL }}{{ data_to_modify.thumbnail.name }}" />

In my view I added 在我看来,我添加了

@never_cache
@cache_control(max_age=0, no_cache=True, no_store=True, must_revalidate=True)

and

    response = render(request, 'mainsite/modify.html', locals(), context_instance=RequestContext(request))   
    response['Cache-Control'] = 'no-store, no-cache, must-revalidate proxy-revalidate'
    response['Expires'] = 'Thu, 01 Jan 1970 00:00:00 GMT'
    response['Pragma'] = 'no-cache'
    return response

But the image is still caching in the browser (Chrome: Version 35.0.1916.153 m)... Do you have any idea how to avoid this? 但是图像仍在浏览器中缓存(Chrome:版本35.0.1916.153 m)...您知道如何避免这种情况吗?

EDIT 1: 编辑1:

**Header:**

Remote Address:127.0.0.1:80
Request URL:http://127.0.0.1/medias/thumbs/odbgelguelteeglndjssastj.jpg
Request Method:GET
Status Code:200 OK (from cache)

As @yedpodtrzitko pointed out, adjusting the cache headers of the web page in Django is pointless. 正如@yedpodtrzitko指出的那样,在Django中调整网页的缓存标头是没有意义的。

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? {{ MEDIA_URL }}{{ data_to_modify.thumbnail.name }}是您要正确显示的图像的URL吗? (you use the same url in the html img tag) (您在html img标签中使用相同的网址)

In that case it doesn't make sense to load that url via ajax. 在这种情况下,通过ajax加载该URL没有任何意义。

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: 似乎您根本不​​需要ajax,只需要定期更新img标签的src属性,并附加cachebuster查询字符串:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM