django静态文件版本控制 [英] django static files versioning

查看:16
本文介绍了django静态文件版本控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一些通用解决方案,以解决静态文件及其中的更新.

I'm working on some universal solution for problem with static files and updates in it.

示例:假设有一个带有/static/styles.css 文件的网站-该网站使用了很长时间-因此很多访问者在浏览器中缓存了该文件

Example: let's say there was site with /static/styles.css file - and site was used for a long time - so a lot of visitors cached this file in browser

现在我们在此css文件中进行更改,并在服务器上进行更新,但是某些用户仍然具有旧版本(尽管服务器返回了修改日期)

Now we doing changes in this css file, and update on server, but some users still have old version (despite modification date returned by server)

显而易见的解决方案是在文件/static/styles.css?v=1.1 中添加一些版本,但是在这种情况下,开发人员必须跟踪该文件中的更改并手动增加版本

The obvious solution is to add some version to file /static/styles.css?v=1.1 but in this case developer must track changes in this file and manually increase version

第二种解决方案是计算文件的md5哈希并将其添加到url /static/styels.css/?v= {mdp5hashvalue} 中,这看起来要好得多,但是md5应该是会以某种方式自动计算出来.

A second solution is to count the md5 hash of the file and add it to the url /static/styels.css/?v={mdp5hashvalue} which looks much better, but md5 should be calculated automatically somehow.

我可能会看到的方式-创建这样的模板标签

they possible way I see it - create some template tag like this

{% static_file  "style.css" %}

这将呈现

<link src="/static/style.css?v=md5hash">

但是,我不希望这个标签在每次页面加载时都计算md5,也不想将哈希存储在django-cache中,因为那样我们在更新文件后就必须清除...

BUT, I do not want this tag to calculate md5 on every page load, and I do not want to store hash in django-cache, because then we will have to clear after updating file...

有什么想法吗?

推荐答案

Django 1.4现在包含 CachedStaticFilesStorage 完全可以满足您的需求(嗯... 几乎).

Django 1.4 now includes CachedStaticFilesStorage which does exactly what you need (well... almost).

自Django 2.2起 <代码应该使用> ManifestStaticFilesStorage 代替 CachedStaticFilesStorage .

Since Django 2.2 ManifestStaticFilesStorage should be used instead of CachedStaticFilesStorage.

您可以将其与 manage.py collectstatic 任务一起使用.与往常一样,所有静态文件都是从您的应用程序中收集的,但是此存储管理器还会创建每个文件的副本,并在名称后附加MD5哈希.例如,假设您有一个 css/styles.css 文件,它也会创建类似 css/styles.55e7cbb9ba48.css 的文件.

You use it with the manage.py collectstatic task. All static files are collected from your applications, as usual, but this storage manager also creates a copy of each file with the MD5 hash appended to the name. So for example, say you have a css/styles.css file, it will also create something like css/styles.55e7cbb9ba48.css.

当然,正如您提到的,问题是您不希望您的视图和模板始终都在计算MD5哈希来查找要生成的适当URL.解决方案是缓存.好的,您要求提供一种无需缓存的解决方案,很抱歉,这就是为什么我说几乎的原因.但是,确实没有理由拒绝缓存. CachedStaticFilesStorage 使用名为 staticfiles 的特定缓存.默认情况下,它将使用您现有的缓存系统,因此!但是,如果您不希望它使用常规缓存,则可能是因为它是分布式内存缓存,并且想要避免仅获取静态文件名的网络查询开销,那么您可以为设置特定的RAM缓存静态文件.这比听起来容易:请查看这个出色的工具博客帖子.如下所示:

Of course, as you mentioned, the problem is that you don't want your views and templates calculating the MD5 hash all the time to find out the appropriate URLs to generate. The solution is caching. Ok, you asked for a solution without caching, I'm sorry, that's why I said almost. But there's no reason to reject caching, really. CachedStaticFilesStorage uses a specific cache named staticfiles. By default, it will use your existing cache system, and voilà! But if you don't want it to use your regular cache, perhaps because it's a distributed memcache and you want to avoid the overhead of network queries just to get static file names, then you can setup a specific RAM cache just for staticfiles. It's easier than it sounds: check out this excellent blog post. Here's what it would look like:

CACHES = {
  'default': {
    'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
    'LOCATION': '127.0.0.1:11211',
  },
  'staticfiles': {
    'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
    'LOCATION': 'staticfiles-filehashes'
  }
}

这篇关于django静态文件版本控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆