본문 바로가기

Web Development/Django

[Django] CSS,JS 파일 캐쉬 해제하는 방법

728x90

웹 배포 시마다 CSS, JS 파일이 바로 적용되지 않는다면, 이전 CSS,JS 파일의 캐쉬가 존재하기 때문이다. 

이를 해제하기 위해서는 다음과 같이 ?{% now 'U' %} 를 붙여준다. 

* static을 사용하지 않는 경우에도 동일하게 href나 src 뒤에 붙여준다

 

<!-- Custom CSS-->
<link rel="stylesheet" type="text/css" href="{% static 'example.css' %}?{% now 'U' %}" >


<!-- Custom Js -->
<script type="text/javascript" src="{% static 'example.js' %}?{% now 'U' %}"></script>

 

?version=1 과 같이 일일이 버전을 수동적으로 관리하는 방법도 있지만,

?{% now 'U' %} 를 사용할 경우, 파일 로드 파라미터로 매 초마다 Unix Timestamp를 자동적으로 생성한다

따라서, 위와 같이 파일 로드 태그를 붙여주면, 실제 웹 사이트 상에서는 아래와 같이 자체적으로 생성한 파라미터(1249948982)를 사용해 파일을 찾게 된다

<link rel="stylesheet" type="text/css" href="{% static 'example.css' %}?1249948982" >

Reference

 

django css file cached

I'm having a css file and i want to modify it to fit my needs. The problem is that it seems to be cached somewhere, as i just cannot see the changes, no matter what i do in the css file. I am sure ...

stackoverflow.com

 

반응형