2012-03-16 22 views
2

Djangoで静的ファイルのバージョンをどのように制御しますか?私はwrote custom templatetagファイルのURLのGETパラメータとして変更日を追加しますが、私がやっているかどうかを知りたいと思います。Djangoの静的ファイルのバージョン

タグのコードは:

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) 
+0

これはパスを返しますが、パスに対応するように実際のファイル名をどのように変更しますか? – silent1mezzo

答えて

7

などdjango-compressorなどのアプリケーション、およびdjango-pipelineは、物事のこれらの並べ替えのために良いです。

+0

+1のdjango-pipelineです。うまく動作し、インストールが簡単です。私はYUI Compressorを使っています。あなたのCSS/jsを結合し、縮小し、バージョンアップします。デバッグモードでは、簡単なデバッグのために、結合されていない/縮小されたファイルを提供します。 –

+0

私はdjango-compressorを使用しており、問題はありませんでした。私はdjango-pipelineについてはわかりませんが、django-compressorはLESS、SASS、CoffeScriptなどの前処理も可能です。 –

関連する問題