2014-06-18 4 views
1

새로 고침, 다음과 같은 자바 스크립트 이미지 새로 고침 :장고 - - 자바 스크립트 이미지에 안 캐시가 내 템플릿에 사용

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

을 그리고 난 다음 HTML 코드를 사용하여 이미지를 표시합니다

<img id="imageactual" src="{{ MEDIA_URL }}{{ data_to_modify.thumbnail.name }}" /> 
을 내보기에서

나는

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

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

그러나 이미지는 브라우저 (Chrome : 버전 35.0.1916.153m)에서 계속 캐싱됩니다. 이것을 피하는 방법을 알고 있습니까?

는 편집 1 : @yedpodtrzitko는 지적

**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) 
+1

그 응답 헤더가 단지 관련 : 전혀 아약스를 필요로하지 않는 것처럼

캐시 버스터 쿼리 문자열이 추가로 당신은 그냥 주기적으로 img 태그의 src 속성을 업데이트 할 필요가 보인다 이미지가 아니라 웹 페이지. 이미지는 다른 요청에 의해 제공되며 응답에는 자체 헤더가 있습니다. 특정 디렉토리/파일 유형에 대한 웹 서버 설정을 수정해야한다고 생각합니다. – yedpodtrzitko

답변

2

, 장고에서 웹 페이지의 캐시 헤더를 조정하는 것은 무의미하다.

이 부분은 잘못된 같습니다

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

{{ MEDIA_URL }}{{ data_to_modify.thumbnail.name }} 당신이 바로 표시 할 이미지의 URL입니다? (당신은 HTML img 태그에서 동일한 URL을 사용합니다)

그런 경우 ajax를 통해 해당 URL을로드하는 것은 의미가 없습니다.

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

매 2 초마다 새로 고침을하려면 캐시 무효화에 100보다 훨씬 큰 숫자를 사용해야합니다. – itsadok

+0

@itsadok ... 좋은 점이 있다면,이 부분은 OP에서 복사하여 붙여 넣었지만'Math.random() '을 직접 사용하지 않고 js 간격만으로 업데이트 빈도를 조절하지 않아도됩니다. – Anentropic

+0

@Anentropic 귀하의 대답은 내 문제를 해결하지만 오래된 이미지는 브라우저 캐시에 남아 있습니다. – Erwan

관련 문제