Fot*_*ian 8 django django-compressor
我很难理解django-compressor的使用.
这是我想要完成的事情:
我想将我的LESS CSS和Coffeescript文件分成资产目录
例如
app
??? assets
??? coffee
? ??? script.coffee
??? less
??? style.less
Run Code Online (Sandbox Code Playgroud)
将静态资产(如图像)保留在静态目录中
例如
app
??? static
??? hello.txt
??? photo.jpg
Run Code Online (Sandbox Code Playgroud)
为此,我已将资源路径添加到我的STATICFILES_DIRS变量,以允许django-compressor查找文件(按预期工作).这是正确的方法吗?我一直试图找到一个专用于django-compressor的独立加载路径,但没有任何运气,因为我不打算将这些资产用作静态.
为了部署到生产,我希望将编译好的CSS和JS文件以及我的app/static目录中的其他媒体(例如图像等)收集到app/static-prod目录中.但这并不是很好,因为在使用collectstatic命令时也会收集资产.
例如
(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.
Run Code Online (Sandbox Code Playgroud)
使用该./manage.py compress
命令只需要记录我编译的文件,而不是本例中的photo.jpg或hello.txt.
我发现这样做的唯一可行方法是使用带有collectstatic的--ignore标志
例如
(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.
Run Code Online (Sandbox Code Playgroud)
我也搞乱了COMPRESS_ROOT和COMPRESS_URL配置变量,但这些只会造成进一步的麻烦.更改COMPRESS_ROOT可解决collectstatic问题,但现在使用compress命令时,生成的文件最终位于静态文件的不同位置.
这些解决方案似乎并不优雅 有一个更好的方法吗?我觉得我错过了什么.
在此先感谢任何帮助:)
我认为我提供了迄今为止我找到的最佳解决方案,但请随意提出更好的替代方案.
阻止我的要求的最大问题是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
Run Code Online (Sandbox Code Playgroud)
除了通常的'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',
)
Run Code Online (Sandbox Code Playgroud)
现在设置一个名为COMPRESS_SOURCE_ROOT的新设置
例如
COMPRESS_SOURCE_ROOT = os.path.join(APP_DIR, 'assets')
Run Code Online (Sandbox Code Playgroud)
我也设置了STATIC_ROOT
STATIC_ROOT = os.path.join(APP_DIR, 'static-prod')
Run Code Online (Sandbox Code Playgroud)
我专门测试了我的LESS源代码编译
(django-cpython)fots@fotsies-ubprecise-01:~/django_learning$ tree app/assets
app/assets
??? coffee
? ??? script.coffee
??? less
??? import.less
??? style.less
Run Code Online (Sandbox Code Playgroud)
使用模板标签
{% compress css %}
<link rel="stylesheet" type="text/less"
href="{{ STATIC_URL }}less/style.less" />
{% endcompress %}
Run Code Online (Sandbox Code Playgroud)
这是从资产目录成功读取的,并在我更改文件时更新.
输出放在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
Run Code Online (Sandbox Code Playgroud)
供您参考,这是我的静态目录的样子
(django-cpython)fots@fotsies-ubprecise-01:~/django_learning$ tree app/static
app/static
??? hello.txt
??? photo.jpg
0 directories, 2 files
Run Code Online (Sandbox Code Playgroud)
所以我们走了
(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
Run Code Online (Sandbox Code Playgroud)
然后,我按如下方式运行网络服务器并确认该网站正常运行
./manage.py runserver 0.0.0.0:8000 --insecure
Run Code Online (Sandbox Code Playgroud)
希望这有助于那里的人:)
归档时间: |
|
查看次数: |
1653 次 |
最近记录: |