ValueError:缺少静态文件清单'favicon.ico'的条目

nal*_*zok 32 python django static django-views django-staticfiles

我正在ValueError跑步的时候python manage.py test.我的项目已命名fellow_go,我正在开发一个名为的应用程序pickup.

请注意,此错误是在相对较新的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'

----------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

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'
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

fellow_go /拾取/ views.py

class HomePageView(TemplateView):

    template_name = 'index.html'
Run Code Online (Sandbox Code Playgroud)

fellow_go /模板/ index.html的

<link rel="icon" href="{% static "favicon.ico" %}">
Run Code Online (Sandbox Code Playgroud)

fellow_go /拾取/测试/ 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)
Run Code Online (Sandbox Code Playgroud)

我有任何favicon.ico文件:

在此输入图像描述


奇怪的是,没有错误发生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
Run Code Online (Sandbox Code Playgroud)

请告诉我我的代码有什么问题.

ema*_*tta 49

试试跑步:

python manage.py collectstatic
Run Code Online (Sandbox Code Playgroud)

测试现在有效吗?如果是这样,这可能是导致问题的配置:

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
Run Code Online (Sandbox Code Playgroud)

相关:https:
//stackoverflow.com/a/32347324/2596187

查看Django文档:https: //docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage.manifest_strict


Cor*_*ory 26

只是分享我对这个问题的解决方案,以防它对其他人有所帮助。

我不小心在一些静态 URL 中包含了前导“/”,这导致它们从清单中丢失。

解决方案是替换以下所有实例:

{% static '/path/to/some/file' %}

{% static 'path/to/some/file' %}

然后一切正常。

  • 不敢相信后端会因此而崩溃。经过无数天的白噪音配置。先生,您刚刚赢得了互联网。 (7认同)
  • 太烦人了,Django 无法正确处理它! (2认同)

dqd*_*dqd 15

如果要继续在Django 1.11(或更新版本)项目中使用WhiteNoise模块,同时防止出现"缺少staticfiles清单条目"错误,则需要manifest_strict通过继承来禁用该属性,如Django文档中所述.

怎么做到这一点?

首先,storage.py在项目目录中创建一个文件:

from whitenoise.storage import CompressedManifestStaticFilesStorage


class WhiteNoiseStaticFilesStorage(CompressedManifestStaticFilesStorage):
    manifest_strict = False
Run Code Online (Sandbox Code Playgroud)

其次,编辑文件中的STATICFILES_STORAGE常量settings.py以指向此新类,例如:

STATICFILES_STORAGE = 'my_project.storage.WhiteNoiseStaticFilesStorage'
Run Code Online (Sandbox Code Playgroud)

  • 现在,您可以直接在白噪声配置中使用此设置,而不必扩展类http://whitenoise.evans.io/en/stable/django.html?highlight=manifest_strict#WHITENOISE_MANIFEST_STRICT (5认同)

Vla*_*mir 11

whitenoise包不一定会发生这种情况。从Django 1.11开始运行测试时,更改STATIC_STORAGEdjango.contrib.staticfiles.storage.ManifestStaticFilesStorage会产生相同的错误。

发生这种情况是因为ManifestStaticFilesStorage期望staticfiles.json存在并包含所要求的文件。您可以通过运行./manage.py collectstatic并重试来确认这一点。

您通常不会在开发中看到此错误,因为当时DEBUG == True,会ManifestStaticFilesStorage切换为非哈希网址。

为了克服这个问题,您必须确保:

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
Run Code Online (Sandbox Code Playgroud)

这是默认值。

一种方法是覆盖测试类的设置:

from django.test import TestCase, override_settings
@override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
class MyTest(TestCase):
    pass
Run Code Online (Sandbox Code Playgroud)

或方法:

from django.test import TestCase, override_settings
class MyTest(TestCase):
    @override_settings(STATICFILES_STORAGE='django.contrib.staticfiles.storage.StaticFilesStorage')
    def test_something(self):
        pass
Run Code Online (Sandbox Code Playgroud)


Eri*_*ael 6

在最新版本的 Whitenoise(当前为 5.2)中,您现在可以执行..

WHITENOISE_MANIFEST_STRICT = False
Run Code Online (Sandbox Code Playgroud)


ynd*_*lok 5

如果您在测试期间引用静态文件,Django 会引发此异常,但./manage.py collectstatic自创建该文件后您尚未运行(请参阅这些文档)。

解决此问题的推荐方法是:

在测试期间,确保将 STATICFILES_STORAGE 设置设置为其他内容,例如“django.contrib.staticfiles.storage.StaticFilesStorage”(默认值)。

这是在您的 中执行此操作的简单方法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'
)
Run Code Online (Sandbox Code Playgroud)


小智 5

我遇到过同样的问题; 每次我使用 DEBUG = False 运行服务器时,都会收到“缺少静态文件清单条目”的信息。

我花了很多时间才弄清楚问题出在静态链接开头包含“/”。

例如: {% static '/app/styles/main.css' %} 而不是 {% static 'app/styles/main.css' %}


归档时间:

查看次数:

18190 次

最近记录:

6 年,3 月 前