【问题标题】:Django doesn't load static files ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)Django 不加载静态文件 ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
【发布时间】:2018-11-12 11:17:17
【问题描述】:

是我在 Django 中的第一个应用程序,我正在尝试为生产准备我的 Django (2.0) 应用程序,但我无法使用 WhiteNoise 正确加载静态文件

我的日志中总是出现下一个错误

ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
ValueError: Missing staticfiles manifest entry for 'css/inicio.css'
[02/Jun/2018 14:40:37] ERROR [django.server:124] "GET /participation/prueba HTTP/1.1" 500 27

我有以下settings.py

...
DEBUG=False
DJANGO_APPS = ['django.contrib.admin',
                'django.contrib.auth',
                'django.contrib.contenttypes',
                'django.contrib.sessions',
                'django.contrib.messages',
                #Delete for development whitenoise.runserver_nostatic
                'whitenoise.runserver_nostatic',
                'django.contrib.staticfiles',
                'django.contrib.sites'
               ]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
...
STATIC_URL = '/static/'

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

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

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

我的所有静态文件都在根级别的名为 static 的文件夹中, 当我运行 manage.py collectstatic 时,我会生成所有静态文件 staticfiles 目录,但不知何故我仍然无法让它运行。

我尝试隔离问题,我正在使用以下模板

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    {% load static %}
    <link rel="stylesheet" href="{% static "css/inicio.css" %}">
</head>
<body>


{% for categoria in categoria_list %}
    <p>
        {{ categoria.titulo }}
    </p>
{% endfor %}


</body>
</html>

我已经尝试将href的路径更改为

{% static "/css/inicio.css" %}
{% static "static/css/inicio.css" %}

但他们都没有加载

我也试过有和没有 'whitenoise.runserver_nostatic' 在 Django Apps 中加载,我一直得到相同的结果。

谁知道我做错了什么?

提前致谢。

【问题讨论】:

    标签: python django production-environment django-staticfiles whitenoise


    【解决方案1】:

    尝试删除此行,

    STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
    

    来源:https://stackoverflow.com/a/32347324/2596187

    【讨论】:

    • 它工作了,但是我无法压缩文件,知道我该如何解决这个问题吗?更新:我使用 django.contrib.staticfiles.storage.ManifestStaticFilesStorage 现在它可以工作了,但我没有看到我的静态文件中有任何压缩完成
    • 你在用heroku吗?
    • @Shivan Singh 短期内是的,后来我想使用亚马逊服务
    • @RobertoFernandezDiaz 我已经为您的问题添加了答案。
    【解决方案2】:

    问题是您在模板中的某处引用了一个不存在的静态文件。具体来说,您将空字符串传递给静态。也许您有类似 {% static some_variable %} 的行,其中 some_variable 未定义?

    在 Django 1.11 中,行为发生了变化,因此丢失的文件会引发错误。见:https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage.manifest_strict

    如果你修复了这个引用,那么一切都会正常。

    下面的问题值得一读。 ValueError at / Missing staticfiles manifest entry for ''

    此答案抄自https://stackoverflow. com/a/49656053/3001007

    另外,这里有一个详尽的答案。 Django Model: ValueError: Missing staticfiles manifest entry for "file_name.ext"

    【讨论】:

      【解决方案3】:

      如果您在 Heroku 上使用 Docker,这是正确的解决方案(而不是完全删除压缩)。之所以会引发该错误,是因为在部署期间未创建 STATIC_ROOT(与普通的 Python/Django 堆栈不同)。此外,您必须确保在所有可能最终提供静态文件的测功机上运行 collectstatic。

      我通过将所有内容烘焙到基础 Docker 映像中来解决此问题。在你的 Dockerfile 中(假设你的 STATIC_ROOT 是 /app/static):

      [...]
      RUN mkdir -p /app/static
      RUN python manage.py collectstatic --no-input
      [...]
      

      这将收集所有静态文件并创建 Whitenoise 用来将普通文件名(例如 css/inicio.css)映射到散列文件名(例如 css/inicio.bf12af51cd.css)的清单文件(用于缓存) .

      【讨论】:

        【解决方案4】:

        如果您要部署到 Azure 应用服务,与 Marco 的回答类似。修改你的启动文件:

        python manage.py migrate
        python manage.py collectstatic
        gunicorn --bind=0.0.0.0 --timeout 30 --max-requests 500 --max-requests-jitter 10  <appname>.wsgi
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-03-10
          • 2017-11-08
          • 1970-01-01
          • 1970-01-01
          • 2012-04-30
          • 2020-12-10
          • 2013-12-09
          • 2021-11-20
          相关资源
          最近更新 更多