Django静态文件版本

ssb*_*sbb 2 python django static

如何在Django中控制静态文件的版本?我写了自定义模板标签,它将修改日期添加为文件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)
Run Code Online (Sandbox Code Playgroud)

sil*_*zzo 7

诸如django-compressordjango-pipeline之类的应用程序适用于这类产品.