Python ValueError:缺少“favicon.ico”的静态文件清单条目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44160666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: Missing staticfiles manifest entry for 'favicon.ico'
提问by nalzok
I'm getting a ValueError
when running python manage.py test
. My project is named fellow_go
, and I'm currently working on an App called pickup
.
我ValueError
在跑步时得到了一个python manage.py test
。我的项目名为fellow_go
,我目前正在开发一个名为 的应用程序pickup
。
Please note that this error is added in a relatively recent commit to Django: Fixed #24452 -- Fixed HashedFilesMixin correctness with nested paths..
请注意,此错误是在相对最近的 Django 提交中添加的:已修复 #24452 -- 使用嵌套路径修复了 HashedFilesMixin 正确性。.
======================================================================
ERROR: test_view_url_exists_at_desired_location (pickup.tests.test_view.HomePageViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/sunqingyao/PycharmProjects/fellow_go/pickup/tests/test_view.py", line 10, in test_view_url_exists_at_desired_location
resp = self.client.get('/', follow=True)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/test/client.py", line 536, in get
**extra)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/test/client.py", line 340, in get
return self.generic('GET', path, secure=secure, **r)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/test/client.py", line 416, in generic
return self.request(**r)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/test/client.py", line 501, in request
six.reraise(*exc_info)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/utils/six.py", line 686, in reraise
raise value
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/core/handlers/base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/core/handlers/base.py", line 215, in _get_response
response = response.render()
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/template/response.py", line 107, in render
self.content = self.rendered_content
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/template/response.py", line 84, in rendered_content
content = template.render(context, self._request)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/test/utils.py", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/template/loader_tags.py", line 177, in render
return compiled_parent._render(context)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/test/utils.py", line 107, in instrumented_test_render
return self.nodelist.render(context)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/templatetags/static.py", line 105, in render
url = self.url(context)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/templatetags/static.py", line 102, in url
return self.handle_simple(path)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/templatetags/static.py", line 117, in handle_simple
return staticfiles_storage.url(path)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 162, in url
return self._url(self.stored_name, name, force)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 141, in _url
hashed_name = hashed_name_func(*args)
File "/Users/sunqingyao/Envs/django_tutorial/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 432, in stored_name
raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
ValueError: Missing staticfiles manifest entry for 'favicon.ico'
----------------------------------------------------------------------
fellow_go/settings.py
Fellow_go/settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
# ......
# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
fellow_go/urls.py
Fellow_go/urls.py
urlpatterns = i18n_patterns(
url(r'^$', HomePageView.as_view(), name='index'),
url(r'^pickup/', include('pickup.urls')),
url(r'^accounts/', include('django.contrib.auth.urls')),
url(r'^admin/', admin.site.urls),
prefix_default_language=False
) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
fellow_go/pickup/views.py
Fellow_go/pickup/views.py
class HomePageView(TemplateView):
template_name = 'index.html'
fellow_go/templates/index.html
Fellow_go/templates/index.html
<link rel="icon" href="{% static "favicon.ico" %}">
fellow_go/pickup/tests/test_view.py
Fellow_go/pickup/tests/test_view.py
class HomePageViewTest(TestCase):
def test_view_url_exists_at_desired_location(self):
resp = self.client.get('/', follow=True)
self.assertEqual(resp.status_code, 200)
Any I do have a favicon.ico
file:
任何我有一个favicon.ico
文件:
Strangely, no errors occur with python manage.py runserver
:
奇怪的是,没有错误发生python manage.py runserver
:
/Users/sunqingyao/Envs/django_tutorial/bin/python3.6 /Users/sunqingyao/PycharmProjects/fellow_go/manage.py runserver 8000
Performing system checks...
System check identified no issues (0 silenced).
May 24, 2017 - 22:09:25
Django version 1.11.1, using settings 'fellow_go.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[24/May/2017 22:09:28] "GET / HTTP/1.1" 200 6276
[24/May/2017 22:09:28] "GET /static/css/style.min.css HTTP/1.1" 200 2474
[24/May/2017 22:09:28] "GET /static/css/ie10-viewport-bug-workaround.css HTTP/1.1" 200 430
[24/May/2017 22:09:28] "GET /static/js/ie10-viewport-bug-workaround.js HTTP/1.1" 200 685
[24/May/2017 22:09:28] "GET /static/js/opt-in.js HTTP/1.1" 200 511
[24/May/2017 22:09:28] "GET /static/css/datetimepicker.css HTTP/1.1" 200 12351
[24/May/2017 22:09:28] "GET /static/js/bootstrap-datetimepicker.js HTTP/1.1" 200 55741
[24/May/2017 22:09:35] "GET /static/favicon.ico HTTP/1.1" 200 766
Not Found: /apple-touch-icon-precomposed.png
[24/May/2017 22:09:35] "GET /apple-touch-icon-precomposed.png HTTP/1.1" 404 2678
Not Found: /apple-touch-icon.png
[24/May/2017 22:09:35] "GET /apple-touch-icon.png HTTP/1.1" 404 2642
Please tell me what's wrong with my code.
请告诉我我的代码有什么问题。
回答by emazzotta
Try running:
尝试运行:
python manage.py collectstatic
Does the test work now? If so, this might be the configuration causing a problem:
现在测试有效吗?如果是这样,这可能是导致问题的配置:
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
Related:
https://stackoverflow.com/a/32347324/2596187
相关:https:
//stackoverflow.com/a/32347324/2596187
Check out the Django documentation: https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage.manifest_strict
查看 Django 文档:https: //docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage.manifest_strict
回答by dqd
If you want to continue to use the WhiteNoisemodule in your Django 1.11 (or newer) project while preventing this "Missing staticfiles manifest entry" error, you need to disable the manifest_strict
attribute by means of inheritance, as noted in Django documentation.
如果您想在 Django 1.11(或更新版本)项目中继续使用WhiteNoise模块,同时防止出现“Missing staticfiles manifest entry”错误,您需要manifest_strict
通过继承禁用该属性,如Django 文档中所述。
How to accomplish that?
如何做到这一点?
Firstly, create a storage.py
file in your project directory:
首先,storage.py
在您的项目目录中创建一个文件:
from whitenoise.storage import CompressedManifestStaticFilesStorage
class WhiteNoiseStaticFilesStorage(CompressedManifestStaticFilesStorage):
manifest_strict = False
Secondly, edit the STATICFILES_STORAGE
constant in your settings.py
file to point to this new class, such as:
其次,编辑文件中的STATICFILES_STORAGE
常量settings.py
以指向这个新类,例如:
STATICFILES_STORAGE = 'my_project.storage.WhiteNoiseStaticFilesStorage'
回答by Vladimir
That not necessarily happens with whitenoise
package. Changing STATIC_STORAGE
to django.contrib.staticfiles.storage.ManifestStaticFilesStorage
will produce the same error while running tests starting with Django 1.11.
whitenoise
包不一定会发生这种情况。从Django 1.11开始运行测试时,更改STATIC_STORAGE
为django.contrib.staticfiles.storage.ManifestStaticFilesStorage
将产生相同的错误。
That happens because ManifestStaticFilesStorage
expects staticfiles.json
to exist and containthe file asked. You can confirm this by running ./manage.py collectstatic
and trying again.
发生这种情况是因为ManifestStaticFilesStorage
期望staticfiles.json
存在并包含所询问的文件。您可以通过运行./manage.py collectstatic
并重试来确认这一点。
You don't generally see this error in development because when DEBUG == True
, ManifestStaticFilesStorage
switches to non-hashedurls.
您通常不会在开发中看到此错误,因为 when DEBUG == True
,ManifestStaticFilesStorage
切换到非散列网址。
To overcome this you've got to make sure that:
要克服这个问题,您必须确保:
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
Which is the default.
这是默认设置。
One way would be to override settings for the test class:
一种方法是覆盖测试类的设置:
from django.test import TestCase, override_settings
@override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
class MyTest(TestCase):
pass
or the method:
或方法:
from django.test import TestCase, override_settings
class MyTest(TestCase):
@override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
def test_something(self):
pass
回答by Cory
Just sharing the solution I had to this issue in case it helps anyone else.
只是分享我对这个问题的解决方案,以防它对其他人有所帮助。
I was accidentally including the leading "/" in some static URLs which caused them to be missing from the manifest.
我不小心在一些静态 URL 中包含了前导“/”,这导致它们从清单中丢失。
The solution was to replace all instances of:
解决方案是替换以下所有实例:
{% static '/path/to/some/file' %}
{% static '/path/to/some/file' %}
with
和
{% static 'path/to/some/file' %}
{% static 'path/to/some/file' %}
and then everything worked properly.
然后一切正常。
回答by Guille Constantino
I had the same issue and I fixed by changing STATICFILES_STORAGE
to:
我遇到了同样的问题,我通过更改STATICFILES_STORAGE
为:
STATICFILES_STORAGE = 'cloudinary_storage.storage.StaticHashedCloudinaryStorage'
Then you should run:
然后你应该运行:
python manage.py collectstatic
回答by resakse
i have this in my setting.py
我的setting.py中有这个
DEBUG = False
try:
from .local_setting import *
except ImportError:
pass
after I removed the try block, everything work again.
在我删除了 try 块后,一切又恢复了。
回答by yndolok
Django raises this exception if you reference a static file during your tests, but you haven't run ./manage.py collectstatic
since creating that file (see these docs).
如果您在测试期间引用静态文件,Django 会引发此异常,但./manage.py collectstatic
自创建该文件后您尚未运行(请参阅这些文档)。
The recommended way to resolve this is:
解决此问题的推荐方法是:
During testing, ensure that the STATICFILES_STORAGE setting is set to something else like 'django.contrib.staticfiles.storage.StaticFilesStorage' (the default).
在测试期间,确保将 STATICFILES_STORAGE 设置设置为其他内容,例如“django.contrib.staticfiles.storage.StaticFilesStorage”(默认值)。
Here's a simple way to do that in your settings.py
:
这是在您的 中执行此操作的简单方法settings.py
:
import sys
TESTING = len(sys.argv) > 1 and sys.argv[1] == 'test'
STATICFILES_STORAGE = (
'django.contrib.staticfiles.storage.StaticFilesStorage'
if TESTING
else 'whitenoise.storage.CompressedManifestStaticFilesStorage'
)
回答by natsuapo
In my case, I am not sure why it happens, but after I removed the heroku
settings django_heroku.settings(locals())
everything worked again.
就我而言,我不确定为什么会发生这种情况,但是在我删除heroku
设置后,django_heroku.settings(locals())
一切又恢复了。