2013-01-14 21 views
8

私はdjango-compressorを使用することに本当に苦労しています。静的ファイルと資産のDjango Compressorとの分離&collectstaticの使用

ここで私が達成しようとしてんだよ:静的ファイルの

分離&資産(LESS、CoffeeScriptの)

私は

資産のディレクトリに自分のLESS CSSとCoffeeScriptのファイルを分離したいと思います

app 
    └── assets 
     ├── coffee 
     │ └── script.coffee 
     └── less 
      └── style.less 

、このような私の静的なディレクトリ内の画像のような静的な資産を残し

例えばこれを行うには

app 
    └── static 
     ├── hello.txt 
     └── photo.jpg 

、私はジャンゴ・コンプレッサは(期待通りに動作します)のファイルを見つけることができるように、私のSTATICFILES_DIRS変数に資産のパスを追加しました。これは正しいアプローチですか?私はdjango-compressor専用の独立したロードパスを見つけようとしていますが、これらのアセットを静的に扱うつもりはないので運がありません。生産に展開するためのプロダクションデプロイメント

用のファイルの

コレクションは、私は、その後コンパイルCSSのように(ように例えば画像や)他の私のアプリ/静的なディレクトリ内のメディアと一緒に& JSファイルがに収集されるだろうapp/static-prodディレクトリ。しかし、collectstaticコマンドを使用しても資産が収集されるため、これはうまくいかない。

./manage.py compressコマンドを使用して

(django-cpython)[email protected]:~/django_learning$ ./manage.py collectstatic --noinput 
Copying '/home/fots/django_learning/app/assets/less/style.less' 
Copying '/home/fots/django_learning/app/assets/less/import.less' 
Copying '/home/fots/django_learning/app/assets/coffee/script.coffee' 
Copying '/home/fots/django_learning/app/static/photo.jpg' 
Copying '/home/fots/django_learning/app/static/hello.txt' 

5 static files copied. 

は私のコンパイルされたファイルではなく、この例では、photo.jpgまたはhello.txtの風袋を取ります。

私は、これは例えばcollectstatic

と--ignoreフラグを使用しているかがわかってきた唯一の可能な方法

(django-cpython)[email protected]:~/django_learning$ ./manage.py collectstatic --noinput --ignore=less --ignore=coffee 
Copying '/home/fots/django_learning/app/static/photo.jpg' 
Copying '/home/fots/django_learning/app/static/hello.txt' 

2 static files copied. 

私もCOMPRESS_ROOTCOMPRESS_URL設定変数の周り混乱しましたが、これらは、さらに問題を引き起こします。 COMPRESS_ROOTを変更すると、集約の問題が解決されますが、現在はcompressコマンドを使用すると、生成されたファイルは静的ファイルとは別の場所にあります。

これらのソリューションはほとんど優雅に見えません。これを行うより良い方法はありますか?私は何かが欠けているように感じる。任意の助けを事前に

感謝:)

答えて

5

私は私がこれまでに見つかったが、より良い代替案を提案すること自由に感じなさいた中で最高のソリューションを提供しようと思いました。

私の要件を妨げる最大の問題は、django-compressorがファインダと出力に同じパスを使用していることです。私が見つけた最良の解決策は次のとおりです。私たちは、最初、私は単にあなたのSTATICFILES_FINDERSにこのファインダーを追加し、この新しいファインダーに

を使用して

from compressor.storage import CompressorFileStorage from compressor.finders import CompressorFinder from compressor.conf import settings class CompressorFileAltStorage(CompressorFileStorage): """ This alternative django-compressor storage class is utilised specifically for CompressorAltFinder which allows an independent find path. The default for ``location`` is ``COMPRESS_SOURCE_ROOT``. """ def __init__(self, location=None, base_url=None, *args, **kwargs): if location is None: location = settings.COMPRESS_SOURCE_ROOT # The base_url is not used by the Finder class so it's irrelevant base_url = None super(CompressorFileAltStorage, self).__init__(location, base_url, *args, **kwargs) class CompressorAltFinder(CompressorFinder): """ A staticfiles finder that looks in COMPRESS_SOURCE_ROOT for compressed files, to be used during development with staticfiles development file server or during deployment. """ storage = CompressorFileAltStorage 

COMPRESS_SOURCE_ROOT

呼び出して、新しい設定に基づいてカスタムファインダーを作成するカスタムファインダー

を作成

通常の「compressor.finders.CompressorFinder」に加えて、を設定してください。

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
# 'django.contrib.staticfiles.finders.DefaultStorageFinder', 
    'mycomp.CompressorAltFinder', 
    'compressor.finders.CompressorFinder', 
) 

とは

例えばCOMPRESS_SOURCE_ROOTと呼ばれる新しい設定を設定します

COMPRESS_SOURCE_ROOT = os.path.join(APP_DIR, 'assets') 

私は、あまりにもテンプレートタグ

STATIC_ROOT = os.path.join(APP_DIR, 'static-prod') 

テスト開発

におけるソリューション

私は特に私のLESSソースコードのコンパイルをテストし

(django-cpython)[email protected]:~/django_learning$ tree app/assets 
app/assets 
├── coffee 
│ └── script.coffee 
└── less 
    ├── import.less 
    └── style.less 

を私STATIC_ROOTを設定しました

{% compress css %} 
    <link rel="stylesheet" type="text/less" 
     href="{{ STATIC_URL }}less/style.less" /> 
{% endcompress %} 

ファイルを変更すると、これがassetsディレクトリから正常に読み込まれ、更新されます。

出力は、静的-prodディレクトリに置かれます。

(django-cpython)[email protected]:~/django_learning$ tree app/static-prod/ 
app/static-prod/ 
└── CACHE 
    ├── css 
    │ ├── style.5abda32cfef7.css 
    │ └── style.6ca1a3d99280.css 
    └── js 
     └── script.8cb4f955df19.js 

3 directories, 3 files 

テストあなたの参考のために制作

のためのソリューションは、ここに私の静的なディレクトリだから

(django-cpython)[email protected]:~/django_learning$ tree app/static 
app/static 
├── hello.txt 
└── photo.jpg 

0 directories, 2 files 

次のようになります。ここに行きます

(django-cpython)[email protected]:~/django_learning$ rm -rf app/static-prod 
(django-cpython)[email protected]:~/django_learning$ ./manage.py collectstatic --noinput 
Copying '/home/fots/django_learning/app/static/photo.jpg' 
Copying '/home/fots/django_learning/app/static/hello.txt' 

2 static files copied. 
(django-cpython)[email protected]:~/django_learning$ ./manage.py compress 
Found 'compress' tags in: 
     /home/fots/django_learning/app/templates/layout.html 
Compressing... done 
Compressed 2 block(s) from 1 template(s). 
(django-cpython)[email protected]:~/django_learning$ tree app/static-prod 
app/static-prod 
├── CACHE 
│ ├── css 
│ │ └── 5abda32cfef7.css 
│ ├── js 
│ │ └── 3b9d1c08d2c5.js 
│ └── manifest.json 
├── hello.txt 
└── photo.jpg 

3 directories, 5 files 

私は、次のようにWebサーバを実行し、サイトは、これはそこに誰かに役立ちます:)

を希望
./manage.py runserver 0.0.0.0:8000 --insecure 

運用であることを確認しました

関連する問題