Django doesn't load static files ValueError(“Missing staticfiles manifest entry for '%s'” % clean_name)

限于喜欢 提交于 2020-12-10 08:47:39

问题


is my first app in Django and I am trying to prepare my Django (2.0) application for production, but I am unable to make the static files load properly using WhiteNoise

I keep having all the time the next error in my log

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

I have the following 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'

I have all my static files in a folder call static at root level, when I run manage.py collectstatic I get generate all the static files in the staticfiles dir, but somehow still I don't manage to make it run.

I try to isolate the problem and I am using the following template

<!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>

I have try already to change the path of href to

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

but none of them have make it load

Also I tried with and without 'whitenoise.runserver_nostatic' loaded in Django Apps and I keep having the same results.

Anyone knows what i am doing wrong ?

Thanks in advance.


回答1:


Try removing this line,

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Source: https://stackoverflow.com/a/32347324/2596187




回答2:


The problem is that somewhere in your templates you're referencing a static file that doesn't exist. Specifically, your passing an empty string to static. Perhaps you have a line like {% static some_variable %} where some_variable is undefined?

In Django 1.11 the behaviour changed so that errors are thrown on missing files. See: https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage.manifest_strict

If you fix this reference then everything should work.

Below SO question is worth read. ValueError at / Missing staticfiles manifest entry for ''

This answer is copied from https://stackoverflow. com/a/49656053/3001007

Also, there is an elaborate answer here. Django Model: ValueError: Missing staticfiles manifest entry for "file_name.ext"




回答3:


If you're using Docker on Heroku this is the correct solution (rather than removing compression altogether). The error is raised simply because the STATIC_ROOT is not being created during deployment (unlike with the normal Python/Django stack). Also you have to make sure to run collectstatic on all dynos that might end up serving static files.

I fixed that by baking everything into the base Docker image. In your Dockerfile (assuming your STATIC_ROOT is /app/static):

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

This will collect all static files and create the manifest file used by Whitenoise to map normal file names (e.g. css/inicio.css) to hashed file names (e.g. css/inicio.bf12af51cd.css) (used for caching).



来源:https://stackoverflow.com/questions/50658241/django-doesnt-load-static-files-valueerrormissing-staticfiles-manifest-entry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!