您如何在Django中控制静态文件的版本?我wrote custom templatetag将修改日期添加为文件URL的GET参数,但想知道-我是否在做。
标签代码:
import os
from django import template
from django.conf import settings
register = template.Library()
@register.simple_tag
def sstatic(path):
'''
Returns absolute URL to static file with versioning.
'''
full_path = os.path.join(settings.STATIC_ROOT, path)
try:
# Get file modification time.
mtime = os.path.getmtime(full_path)
return '%s%s?%s' % (settings.STATIC_URL, path, mtime)
except OSError:
# Returns normal url if this file was not found in filesystem.
return '%s%s' % (settings.STATIC_URL, path)
django-compressor和django-pipeline之类的应用程序对这类事情有好处。
Django 1.7添加了ManifestStaticFilesStorage,其设计为通过在文件名后附加哈希将其作为collectstatic
循环的一部分。默认情况下,它适用于所有文件类型。
如果您要使用它,但要指定版本的文件类型,那么我写了an extension,该文件允许您通过路径模式将文件列入黑名单/黑名单。
自Django 2.2起,仍建议将ManifestStaticFilesStorage用作此方法。