gpt4 book ai didi

python - 带有压缩静态文件的 Django Whitenoise

转载 作者:行者123 更新时间:2023-12-05 06:23:01 33 4
gpt4 key购买 nike

我无法让我的 django 项目与 whitenoise 和压缩的静态文件(包括 libsass)一起运行。在下面的链接中,我读到只有通过离线压缩所需的静态文件才有可能。但是当我启动 docker 容器时,运行 compress 命令

docker-compose -f production.yml run --rm django python manage.py compress

给我错误:

ValueError: Missing staticfiles manifest entry for 'sass/app.scss'

在尝试请求网站时出现以下错误(如预期?):

compressor.exceptions.OfflineGenerationError: You have offline compression enabled but key "..." is missing from offline manifest. You may need to run "python manage.py compress"

设置如下(使用 cookiecutter-django 构建,完整代码库见下方链接):

STATIC_ROOT = str(ROOT_DIR("staticfiles"))
STATIC_URL = "/static/"
STATICFILES_DIRS = [str(APPS_DIR.path("static"))]
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]

STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"

STATICFILES_FINDERS += ["compressor.finders.CompressorFinder"]

COMPRESS_PRECOMPILERS = [("text/x-scss", "django_libsass.SassCompiler")]
COMPRESS_CACHEABLE_PRECOMPILERS = (("text/x-scss", "django_libsass.SassCompiler"),)

COMPRESS_ENABLED = env.bool("COMPRESS_ENABLED", default=True)
COMPRESS_STORAGE = "compressor.storage.GzipCompressorFileStorage"
COMPRESS_URL = STATIC_URL

所以在互联网上搜索了 1 天之后;我被卡住了...感谢任何帮助或建议!

代码库:https://github.com/rl-institut/E_Metrobus/tree/compress

这是用 cookiecutter-django-foundation 构建的

包括对 config/setttings/production.py 的以下更改:

COMPRESS_STORAGE = "compressor.storage.GzipCompressorFileStorage"  # Instead of pre-set "storages.backends.s3boto3.S3Boto3Storage"
COMPRESS_ROOT = STATIC_ROOT # Just in case
COMPRESS_OFFLINE = True # Needed to run compress offline

可能的相关链接:

编辑

使用 Justins answer 解决了它(见下文,有额外的更改)。我的错误是试图用已经运行的容器压缩文件,给我上面的错误。使用以下行更改 Dockerfile 后(注意重复的 collectstatic cmd!):

python /app/manage.py collectstatic --noinput
python /app/manage.py compress --force
python /app/manage.py collectstatic --noinput
/usr/local/bin/gunicorn config.wsgi --bind 0.0.0.0:5000 --chdir=/app

并重建图像,一切都很顺利 :)此外,与上述设置不同,我必须在我的设置/env 文件中设置 COMPRESS_ENABLED=True

最佳答案

我刚刚遇到了同样的问题。

将此添加到 project/compose/production/django/start

python /app/manage.py compress --force

python /app/manage.py collectstatic --noinput
python /app/manage.py compress --force
/usr/local/bin/gunicorn config.wsgi --bind 0.0.0.0:5000 --chdir=/app

关于python - 带有压缩静态文件的 Django Whitenoise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58712195/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com