[英]Django doesn't load static files ValueError(“Missing staticfiles manifest entry for '%s'” % clean_name)
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是我在 Django 中的第一个应用程序,我正在尝试为生产准备我的 Django (2.0) 应用程序,但我无法使用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我有以下 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.我将所有静态文件都放在根级别调用 static 的文件夹中,当我运行 manage.py collectstatic 时,我会在 staticfiles 目录中生成所有静态文件,但不知何故我仍然无法使其运行。
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我已经尝试将 href 的路径更改为
{% 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.此外,我尝试在 Django Apps 中加载和不加载 'whitenoise.runserver_nostatic' ,我一直得到相同的结果。
Anyone knows what i am doing wrong ?有谁知道我做错了什么?
Thanks in advance.提前致谢。
Try removing this line,尝试删除此行,
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
Source: https://stackoverflow.com/a/32347324/2596187来源: https : //stackoverflow.com/a/32347324/2596187
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?
也许你有一行像 {% static some_variable %} 的 some_variable 是未定义的?
In Django 1.11 the behaviour changed so that errors are thrown on missing files.在 Django 1.11 中,行为发生了变化,因此会在丢失的文件上引发错误。 See: https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage.manifest_strict
请参阅: 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.以下SO问题值得一读。 ValueError at / Missing staticfiles manifest entry for ''
ValueError at / Missing staticfiles manifest entry for ''
This answer is copied from https://stackoverflow.这个答案是从https://stackoverflow复制的。 com/a/49656053/3001007
com/a/49656053/3001007
Also, there is an elaborate answer here.此外,这里有一个详尽的答案。 Django Model: ValueError: Missing staticfiles manifest entry for "file_name.ext"
Django 模型:ValueError:缺少“file_name.ext”的静态文件清单条目
If you're using Docker on Heroku this is the correct solution (rather than removing compression altogether).如果您在 Heroku 上使用 Docker,这是正确的解决方案(而不是完全删除压缩)。 The error is raised simply because the
STATIC_ROOT
is not being created during deployment (unlike with the normal Python/Django stack).引发该错误的原因很简单,因为在部署期间未创建
STATIC_ROOT
(与普通的 Python/Django 堆栈不同)。 Also you have to make sure to run collectstatic
on all dynos that might end up serving static files.此外,您必须确保在所有可能最终提供静态文件的 dyno 上运行
collectstatic
。
I fixed that by baking everything into the base Docker image.我通过将所有内容烘焙到基础 Docker 映像中来解决这个问题。 In your Dockerfile (assuming your
STATIC_ROOT
is /app/static
):在您的 Dockerfile 中(假设您的
STATIC_ROOT
是/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 (eg css/inicio.css
) to hashed file names (eg css/inicio.bf12af51cd.css
) (used for caching).这将收集所有静态文件并创建 Whitenoise 使用的清单文件,以将普通文件名(例如
css/inicio.css
) css/inicio.css
到散列文件名(例如css/inicio.bf12af51cd.css
)(用于缓存)。
Similarly to Marco's answer, if you are deploying to an Azure App Service.与 Marco 的回答类似,如果您要部署到 Azure 应用服务。 Modify your startup file to :
将您的启动文件修改为:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.