使用{%static%}进行Django压缩

Dio*_*lor 4 django django-static django-staticfiles django-compressor

我正在努力使django压缩工作,但我相信它不起作用因为我的{% static %}使用.

我的模板是(我正在使用pyjade,但无关紧要):

- load staticfiles
- load compress

| {% compress css %}
link(rel="stylesheet", href="{% static 'less/bootstrap.css' %} ")
link(rel="stylesheet", href="{% static 'timepicker/css/bootstrap-timepicker.min.css'%}")
link(rel="stylesheet", href="{% static 'leaflet/addons/locatecontrol/L.Control.Locate.css' %} ")
link(rel="stylesheet", href="http://cdn.leafletjs.com/leaflet-0.6.4/leaflet.css")
link(href='//api.tiles.mapbox.com/mapbox.js/v1.3.1/mapbox.css', rel='stylesheet')
| {% endcompress %}
Run Code Online (Sandbox Code Playgroud)

还是我的settings.py的一部分:

PROJECT_DIR = os.path.dirname(os.path.realpath(__file__))

STATIC_ROOT = os.path.join(PROJECT_DIR, '../static')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR, 'media'),
)

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

COMPRESS_URL = STATIC_URL
COMPRESS_ROOT = STATIC_ROOT
COMPRESS_STORAGE = "staticfiles.storage.StaticFileStorage"

INSTALLED_APPS = (....,'compressor',....)
Run Code Online (Sandbox Code Playgroud)

即使我$ python manage.py collectstatic压缩不起作用并吐出原始文件.在文档中它说我应该提供我认为我给出的绝对路径,不是吗?有人可以帮助压缩工作吗?谢谢.(我不是很熟悉django的静态文件).


更新

在关注Timmy的评论后,我在设置中启用COMPRESS_ENABLED = True(和DEBUG=False),它仍然需要找到文件:

UncompressableFileError at /
'less/bootstrap.css ' could not be found in the COMPRESS_ROOT '/Users/diolor/mysite/wsgi/static' or with staticfiles.
Run Code Online (Sandbox Code Playgroud)

请注意,正确地找到并呈现静态文件(何时COMPRESS_ENABLED = False).

我的结构:

mysite/
   wsgi/
      myapp/
         settings.py
         manage.py
         media/
            #js & css files
      static/
         [...]
Run Code Online (Sandbox Code Playgroud)

玩了一段时间后,看起来压缩有css和麻烦{% static %}.

如果你有 link(rel="stylesheet", href="/static/less/bootstrap.css") 它压缩样式表, link(rel="stylesheet", href="{% static 'less/bootstrap.css' %} ")就会产生错误.

在js上,它渲染得很好: script(type="text/javascript", src='{% static "bootstrap/js/bootstrap.min.js" %}')

Aro*_*fis 9

问题是你在href的末尾有一个空格,在%}和之间".如果仔细查看错误消息,您将看到压缩器正在查找末尾有空格的文件.(传单样式表上的内容相同.)