7

My django.contrib.staticfiles settings seems to be ok as all static files get served as expected. However, eg. /static/*.css files do not get gzipped although I have GZipMiddleware turned on.

Fyi. my views html actually does get gzipped, only the files served by the staticfiles app dont. Seems these responses dont go through the middleware chain?

Carsten
  • 539
  • 4
  • 10

4 Answers4

7

The trick is to have the development server run with the '--nostatic' flag set: ./manage.py runserver --nostatic.

One then can user a url pattern for serving the static files like so:

if settings.DEBUG:
    static_pattern = r'^%s(?P<path>.*)$' % (settings.STATIC_URL[1:],)
    urlpatterns += patterns('django.contrib.staticfiles.views',
        url(static_pattern, 'serve', {'show_indexes': True}),
    )

When run without --nostatic, django will automatically serve things under STATIC_URL without going through the middleware chain.

Thanks to Dave for his pointers!

Carsten
  • 539
  • 4
  • 10
2

Is it possible you don't have the GZipMiddleware AT THE TOP of your settings.MIDDLEWARE_CLASSES? That might cause weird behavior.

If this is a production server, though, you probably shouldn't be serving static files with django at all. I'd recommend gunicorn and nginx.

EDIT: If not that, what if you serve the files "manually" via urls.py, using something like:

urlpatterns += staticfiles_urlpatterns() + \
        patterns('',
            (r'^%s/(?P<path>.*)$' % settings.MEDIA_URL.strip('/'), 'django.views.static.serve', {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
            *[(r'^%s/(?P<path>.*)$' % settings.STATIC_URL.strip('/'), 'django.views.static.serve', {'document_root': path, 'show_indexes': True}) for path in settings.STATICFILES_DIRS]
        )

Alternative #3: Nginx is pretty easy to install locally, and you could just point it at your Django server (no need for gunicorn/uwsgi/whatever).

Dave
  • 11,499
  • 5
  • 34
  • 46
  • Yea, GZipMiddleWare is at the top. No, like I wrote in the subject, my question is about dev mode. In production, I also use nginx and have compression etc done there. Now Id also be interested in seeing the aproximate filesizes of my static assets while developing. – Carsten Sep 28 '11 at 08:53
  • @Carsten, you may wish to mention 'dev mode' in the question content too... because it totally took me 30 seconds AFTER reading this comment to find it : ) I guess some of us don't read the titles after we click on them! – Dave Sep 28 '11 at 17:37
  • @Carsten, I added two more things you could try. – Dave Sep 28 '11 at 17:39
0

additional for Carsten's answer https://stackoverflow.com/a/7673706/8137384

Modern versions contains the helper:

from django.conf.urls.static import static

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
0

In production environment your webserver (Apache/Nginx/IIS) takes care of gzipping static, so doesn't matter whether gzip works in dev or not.

Marat
  • 15,215
  • 2
  • 39
  • 48