代码之家  ›  专栏  ›  技术社区  ›  Roberto Fernandez Diaz

django不加载静态文件valueerror(“缺少%s的static files清单项””%clean\u name)

  •  2
  • Roberto Fernandez Diaz  · 技术社区  · 7 年前

    是我在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
    

    我有以下设置.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'
    

    我把所有的静态文件都放在根级别的静态文件夹中, 当我运行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" %}
    

    但没有一个能装得下

    我也试过有没有 '白噪声。运行服务器' 在django应用程序中加载,我一直有相同的结果。

    有人知道我做错了什么吗?

    提前谢谢。

    2 回复  |  直到 7 年前
        1
  •  5
  •   Shivam Singh    7 年前

    尝试删除这条线,

    STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
    

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

        2
  •  1
  •   Ajeeb.K.P    6 年前

    问题是模板中的某个地方引用了一个不存在的静态文件。特别是,将空字符串传递给static。也许您有一行{%static some_variable%},其中有些_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"