8

I'm having a really hard time getting my head around the use of django-compressor.

Here's what I'm attempting to accomplish:

Separation of Static Files & Assets (LESS, Coffeescript)

I would like to separate my LESS CSS and Coffeescript files into an assets directory

e.g.

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

Leaving static assets such as images in my static directory

e.g.

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

To do this, I've added the assets path to my STATICFILES_DIRS variable to allow django-compressor to find the files (which works as expected). Is this the correct approach? I have been trying to find an independent load path dedicated to django-compressor but didn't have any luck as I don't intend for these assets to be served as statics.

Collection of Files for Production Deployment

For deploying to production, I would then like the compiled CSS & JS files along with other media in my app/static directory (e.g. images and so on) to be collected to an app/static-prod directory. But this doesn't work out so well because the assets are also collected when using the collectstatic command.

e.g.

(django-cpython)fots@fotsies-ubprecise-01:~/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.

Using the ./manage.py compress command only takes tare of my compiled files, not photo.jpg or hello.txt in this example.

The only possible way I've found to do this is using the --ignore flag with collectstatic

e.g.

(django-cpython)fots@fotsies-ubprecise-01:~/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.

I've also messed around with the COMPRESS_ROOT and COMPRESS_URL configuration variables but these only cause further trouble. Changing COMPRESS_ROOT resolves the collectstatic issue but now when using the compress command, the generated files end up in a different location to the static files.

These solutions hardly seem elegant. Is there a better way to do this? I feel like I'm missing something.

Thanks in advance for any help :)

4

1 回答 1

6

我想我会提供迄今为止我发现的最佳解决方案,但请随时提出更好的替代方案。

阻止我要求的最大问题是 django-compressor 对其查找器和输出使用相同的路径。我找到的最佳解决方案如下。

创建自定义查找器

我们首先根据我称为 COMPRESS_SOURCE_ROOT 的新设置创建自定义查找器

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

使用这个新的取景器

除了通常的“compressor.finders.CompressorFinder”之外,只需将此查找器添加到您的STATICFILES_FINDERS设置中

例如

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

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

在开发中测试解决方案

我专门测试了我的LESS源代码编译

(django-cpython)fots@fotsies-ubprecise-01:~/django_learning$ tree app/assets
app/assets
├── coffee
│   └── script.coffee
└── less
    ├── import.less
    └── style.less

带有模板标签

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

这是从资产目录成功读取并在我更改文件时更新的。

输出放置在 static-prod 目录中:

(django-cpython)fots@fotsies-ubprecise-01:~/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)fots@fotsies-ubprecise-01:~/django_learning$ tree app/static
app/static
├── hello.txt
└── photo.jpg

0 directories, 2 files

所以我们开始

(django-cpython)fots@fotsies-ubprecise-01:~/django_learning$ rm -rf app/static-prod
(django-cpython)fots@fotsies-ubprecise-01:~/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)fots@fotsies-ubprecise-01:~/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)fots@fotsies-ubprecise-01:~/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

然后我按如下方式运行网络服务器并确认该站点可以运行

./manage.py runserver 0.0.0.0:8000 --insecure

希望这可以帮助那里的人:)

于 2013-01-14T23:51:34.840 回答