Whitenoise和Django压缩程序导致404压缩文件 [英] Whitenoise and django-compressor cause 404 for compressed files

查看:103
本文介绍了Whitenoise和Django压缩程序导致404压缩文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用 whitenoise django-compressor 将Django应用程序部署到Heroku.

Trying to deploy a Django application to Heroku using whitenoise and django-compressor.

使用DEBUG = FalseCOMPRESS_ENABLED = True将其部署到生产环境中,可以无问题地访问我的所有静态资产.但是,所有压缩文件都返回404,例如:

Deploying it to production with DEBUG = False and COMPRESS_ENABLED = True, all my static assets can be accessed without a problem. However, all the compressed files return a 404, e.g.:

http://*.herokuapp.com/static/CACHE/css/fbfaa35dc638.css失败 加载资源:服务器的响应状态为404(找不到)

http://*.herokuapp.com/static/CACHE/css/fbfaa35dc638.css Failed to load resource: the server responded with a status of 404 (NOT FOUND)

启用DEBUG或禁用COMPRESS_ENABLED都可以解决问题,但是当然不是我想要的.

Either enabling DEBUG or disabling COMPRESS_ENABLED fixes the problem, but of course is not what I want.

我也在设置STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage',但是更改此设置无济于事.

I'm also setting STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage', but changing this doesn't help.

一些设置(请注意,我有一个包含base.pylocal.py等的设置目录,这就是为什么我在路径上需要一个额外的../的原因):

Some settings (note I have a settings directory with e.g. base.py, local.py, etc. that's why I need an extra ../ on the paths):

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, '../staticfiles')

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, '../static'),
)

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

在我的基本模板中:

{% compress css %}
<link rel="stylesheet" href="{% static 'css/app.css' %}">
{% block css %}{% endblock %}
{% endcompress %}

[...]

{% compress js %}
<script src="{% static 'js/main.js' %}"></script>
{% block js %}{% endblock js %}
{% endcompress %}

同样,将它们移出压缩块可解决此问题.只是找不到压缩文件.

Again, moving them out of the compress blocks fixes the issue. Just the compressed files can't be found.

有什么想法吗?

编辑

我忘了提及我根据部署而添加的一项设置清单,即: https://docs.djangoproject .com/en/1.8/ref/templates/api/#django.template.loaders.cached.Loader

I forgot to mention one setting I added as per the deployment checklist, namely this: https://docs.djangoproject.com/en/1.8/ref/templates/api/#django.template.loaders.cached.Loader

TEMPLATES[0]['OPTIONS']['loaders'] = [
    (
        'django.template.loaders.cached.Loader', [
            'django.template.loaders.filesystem.Loader',
            'django.template.loaders.app_directories.Loader',
        ]
    ),
]

删除此设置可使页面再次工作.但是,JS和CSS文件未压缩...怎么回事?

Removing this setting makes the page work again. HOWEVER, the JS and CSS files are not compressed... What's going on?

编辑2

这不是在Heroku上找不到的Django静态文件的重复副本(白噪声):

  • 我的问题出在django-compressor,而不是白噪声
  • 他们得到的不是404,而是500.
  • 他们的问题是他们忘记运行collectstatic ...在这里不是这种情况.

推荐答案

这似乎是一个已知问题.

This seems to be a known problem.

根据Squebe-"Compressor的在线模式在Heroku上不起作用,因为Whitenoise仅在加载应用程序时检查静态文件夹(请参阅我对问题#680的评论).要在生产中使用压缩机,我认为您需要使用在离线模式下运行它,并在使用Heroku的后编译钩子加载应用之前运行python manage.py compress."

According to Squebe - "Compressor's Online mode doesn't work on Heroku because Whitenoise only checks the static folder when the app is loaded (see my comment on issue #680). To use compressor in production I think you need to use it in Offline mode and run python manage.py compress before the app loads using Heroku's post compile hook."

无法通过此方法在Heroku#486上运行

这篇关于Whitenoise和Django压缩程序导致404压缩文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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