0

I've a django website set up using django-compressor + memcached. Not sure when it started, but I'm finding new css and js files in .../collect_static/CACHE/css and .../collect_static/CACHE/js every minute, like output.2fde5b60eff0.css.

I use django.contrib.staticfiles.storage.ManifestStaticFilesStorage.

I have no clue if this is normal, or happening because of some misconfiguration. But in every few days, I need to clean the server because of this.

Any suggestions what is going on here?

Update: It seems to be happening because of template variables inside css and js code, as per this answer, but as I've a lot of such variables, I still don't know how to fix this.

manisar
  • 103
  • 1
  • 7

1 Answers1

0

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:

  1. Storage keys for the third party storage service (such as Google or Amazon)
  2. 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.

manisar
  • 103
  • 1
  • 7