Django:如何在开发模式下gzip提供静态文件? [英] Django: How can I gzip staticfiles served in dev mode?

查看:363
本文介绍了Django:如何在开发模式下gzip提供静态文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的django.contrib.staticfiles设置似乎是确定的,因为所有的静态文件都按预期方式得到满足。
但是,例如/static/*.css文件不会被gzip压缩,虽然我已经启用了GZipMiddleware。

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。我的意见html实际上得到gzip压缩,只有静态文件服务的文件没有。似乎这些回答不会通过中间件链?

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?

推荐答案

诀窍是让开发服务器运行--nostatic 'flag set: ./ manage.py runserver --nostatic

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

然后可以使用一个url模式提供静态文件,如下所示:

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}),
    )

当不运行--noostatic时, django将自动为STATIC_URL提供服务,而无需通过中间件链。

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

感谢Dave为他的指针!

Thanks to Dave for his pointers!

这篇关于Django:如何在开发模式下gzip提供静态文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆