静态文件的分离&使用Django Compressor&使用collectstatic

时间:2013-01-14 08:59:12

标签: django django-compressor

我很难解决使用django-compressor的问题。

这是我想要完成的事情:

静态文件的分离&资产(LESS,Coffeescript)

我想将我的LESS CSS和Coffeescript文件分成资产目录

e.g。

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

在静态目录中保留图像等静态资源

e.g。

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

为此,我已将资源路径添加到 STATICFILES_DIRS 变量,以允许django-compressor查找文件(按预期工作)。这是正确的方法吗?我一直试图找到专用于django-compressor的独立加载路径,但没有任何运气,因为我不打算将这些资产用作静态。

生产部署文件集合

为了部署到生产,我会喜欢编译的CSS& JS文件以及我的app /静态目录中的其他媒体(例如图像等)将被收集到app / static-prod目录中。但这并不是很好,因为在使用collectstatic命令时也会收集资产。

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.

使用./manage.py compress命令只记录我编译的文件,而不是本例中的photo.jpg或hello.txt。

我发现这样做的唯一可行方法是将--ignore标志与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.

我还搞乱了 COMPRESS_ROOT COMPRESS_URL 配置变量,但这些只会造成进一步的麻烦。更改COMPRESS_ROOT可解决collectstatic问题,但现在使用compress命令时,生成的文件最终位于静态文件的不同位置。

这些解决方案似乎并不优雅。有一个更好的方法吗?我觉得我错过了什么。

提前感谢您的任何帮助:)

1 个答案:

答案 0 :(得分:5)

我认为我提供了迄今为止我找到的最佳解决方案,但请随时提出更好的替代方案。

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

创建自定义查找程序

我们首先根据我调用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

使用这个新的查找程序

除了通常的'compress.finders.CompressorFinder'

之外,只需将此查找器添加到 STATICFILES_FINDERS 设置

e.g。

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 的新设置

e.g。

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

希望这可以帮助那些人:)