ITPub博客

首页 > Linux操作系统 > Linux操作系统 > File hash GET parameters for Django staticfiles

File hash GET parameters for Django staticfiles

原创 Linux操作系统 作者:jieforest 时间:2012-08-03 01:35:09 0 删除 编辑
About
A class for the static files app in Django that invalidates outdated browser cache.

How to use
You add the line

CODE:

STATICFILES_STORAGE = 'path.to.hashpathstaticfilesstorage.HashPathStaticFilesStorage'to your settings.py and everywhere you use the 'static' templatetag it will append a hash calculated form. the contents of the file as a GET parameter at the end of the URL for the file. Example:

CODE:

{% static "path/to/file.txt" %} -> /static/path/to/file.txt?4e1243

This guarantees that every time you update your static files, whether it is an image, a CSS file or anything else, all browsers fetch the new version of the file instead of using their cached versions.

Remember to change the import path to match your setup (replace the 'path.to' with your actual import path).

[b]Caching[/b]

You can reduce the time it takes to return the URL by caching the hashes when they are first calculated. The cache framework that ships with Django is used to store the hashes, so make sure you configure that before caching your hashes.

When you start using the cache, and every time after that when you have to invalidate the cache, you simply create an object of the type HashPathStaticFilesStorage and call the method 'invalidate_cache' on that object:

[code]
h = HashPathStaticFilesStorage()
h.invalidate_cache()
[/code]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/301743/viewspace-739707/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-04-23

  • 博文量
    441
  • 访问量
    602349