Ok, so I found the underlying reason.
It is not actually the presence of template variables like {{context_data_var}}
within compressed code.
It is the presence of any such variables the values of which change on each request.
I had two such instances:
- Storage keys for the third party storage service (such as Google or Amazon)
- csrf tokens used in various ajax requests
For 1. above, I simply moved such code outside compress.
For 2., the solution is slightly involved. I had to move away from using {{csrf_token}}
. Django explains it in detail here. We need to use the csrftoken
cookie instead of the variable {{csrf_token}}
, and django sets this cookie if there is at least one {% csrf_token %}
in the template. I had one luckily in my base template, so the cookie was already getting set for me. I also had the getCookie()
function defined for all pages.
Thus, I was able to get rid of the issue explained in my question.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…