Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Django compress with {% static %}

I'm trying to make django compress work but I believe it does not work because of my {% static %} use.

My template is (I'm using pyjade but doesn't matter):

- 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 %}

And part of my 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',....)

Even if I $ python manage.py collectstatic the compress does not work and spits out the original files. In the docs it says I should provide the absolute path which I think I have given, haven't I? Can someone help make compress work? Thanks. (I'm not an very familiar with django's static files).


Update

After following Timmy's comment I enabled COMPRESS_ENABLED = True (and DEBUG=False) in settings, it still needs to find the files:

UncompressableFileError at /
'less/bootstrap.css ' could not be found in the COMPRESS_ROOT '/Users/diolor/mysite/wsgi/static' or with staticfiles.

Just to note that the static files are correctly found and rendered (when COMPRESS_ENABLED = False).

My structure:

mysite/
   wsgi/
      myapp/
         settings.py
         manage.py
         media/
            #js & css files
      static/
         [...]

After playing some time it looks like compress has trouble with the css and {% static %}.

If you have link(rel="stylesheet", href="/static/less/bootstrap.css") it greaty compresses the stylesheets, on link(rel="stylesheet", href="{% static 'less/bootstrap.css' %} ") it raises an error.

On js, it renders it fine: script(type="text/javascript", src='{% static "bootstrap/js/bootstrap.min.js" %}')

like image 472
Diolor Avatar asked Jan 13 '23 00:01

Diolor


1 Answers

The problem is that you have a space at the end of your href, between %} and ". If you look at the error message carefully, you'll see that the compressor is looking for a file with a space at the end. (Same thing on the leaflet stylesheet.)

like image 144
Aron Griffis Avatar answered Jan 19 '23 07:01

Aron Griffis