ValueError: Missing staticfiles manifest entry for 'favicon.ico'
Asked Answered
U

17

94

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.

Please note that this error is added in a relatively recent commit to Django: Fixed #24452 -- Fixed HashedFilesMixin correctness with nested paths..

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

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

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

class HomePageView(TemplateView):

    template_name = 'index.html'

fellow_go/templates/index.html

<link rel="icon" href="{% static "favicon.ico" %}">

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:

enter image description here


Strangely, no errors occur with 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.

Unprintable answered 24/5, 2017 at 14:1 Comment(0)
Y
113

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'

as of whitenoise v4 this will fail and you should use:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

Related:
https://mcmap.net/q/100687/-39-collectstatic-39-command-fails-when-whitenoise-is-enabled

Check out the Django documentation: https://docs.djangoproject.com/en/1.11/ref/contrib/staticfiles/#django.contrib.staticfiles.storage.ManifestStaticFilesStorage.manifest_strict

Note that support for STATICFILES_STORAGE was deprecated and then removed from Django 5.1.

Yeeyegg answered 25/5, 2017 at 10:55 Comment(6)
I found this useful. #50009555Homerus
Run "python manage.py collecstatic" (again) and refresh the website, should work.Jocelyn
Just to complement @Yeeyegg 's answer. I had to run heroku run python3 manage.py collectstatic and it worked for me.Obara
this option was removed in whitenoise v4 whitenoise.evans.io/en/stable/changelog.html#v4-0 please advise on how to fix the issue using v4Position
The OP takes care to point out that they "do have a favicon.ico file," but here is a reminder to double-check that the file is where it's supposed to be before re-running collectstatic. I somehow bungled the file transfer process during deployment such that the favicon file was left behind, while all the other images were where they should be. The site ran fine with DEBUG set to True but broke with debugging disabled, with the same error the OP got. Re-running collectstatic didn't help till the favicon got put in the static dir, as specified in settings.Umbilical
Using Whitenoise 6.6.0, I had to add an explicit ./manage.py collectstatic call to get our Django-based tests working in CI, before the ./manage.py test call. With this in place, it works nicely now.Vano
W
58

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.

The solution was to replace all instances of:

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

with

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

and then everything worked properly.

Wyon answered 21/10, 2019 at 7:49 Comment(4)
Can't believe the backend can broke because of this. After endless days with whitenoise configs. You sir just won the internet.Purnell
So annoying that Django can't properly handle it!Wyon
Nice, would not have though of that in a million years haha - Not for future reader only had this issue after setting up whitenoise... when I handle the static files in nginx this didn't matterStephaniastephanie
This was my issue too. I had to use my IDE's 'global site' search for "{% static '/" to find the faulty static link!Devereux
M
43

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.

That happens because ManifestStaticFilesStorage expects staticfiles.json to exist and contain the file asked. You can confirm this by running ./manage.py collectstatic and trying again.

You don't generally see this error in development because when DEBUG == True, ManifestStaticFilesStorage switches to non-hashed urls.

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
Meson answered 27/6, 2018 at 10:13 Comment(2)
In my case, simply running python collectstatic took care of the issue I was seeing locally. Doh.Novelia
Support for STATICFILES_STORAGE was removed from Django 5.1Sup
T
34

If you want to continue to use the WhiteNoise module 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.

How to accomplish that?

Firstly, create a storage.py file in your project directory:

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 = 'my_project.storage.WhiteNoiseStaticFilesStorage'
Timi answered 29/7, 2018 at 12:28 Comment(1)
This is now a setting you can use directly in your whitenoise config, instead of having to extend the class whitenoise.evans.io/en/stable/…Acquiescence
N
11

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).

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).

Here's a simple way to do that in your 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'
)
Nonsuit answered 9/10, 2019 at 8:14 Comment(1)
Support for STATICFILES_STORAGE was removed from Django 5.1Sup
E
6

In the latest version of Whitenoise (currently 5.2) you can now do..

WHITENOISE_MANIFEST_STRICT = False
Electrodialysis answered 18/9, 2020 at 0:58 Comment(1)
Saw this in the Whitenoise docs too, but did not work for me. To test this, intentionally reference a missing file while using whitenoise.storage.CompressedManifestStaticFilesStorage. For me, running Django==3.1.2 and the same version of Whitenoise you mentioned, the error still persists.Tradesman
C
6

I had the same issue; where I was getting "Missing static files manifest entry for" every time I ran my server with DEBUG = False.

It took me many hours to figure out that the problem was with including "/" at the start of my static links.

For example: {% static '/app/styles/main.css' %} instead of {% static 'app/styles/main.css' %}

Copestone answered 6/6, 2021 at 19:57 Comment(0)
R
2

env: Python 3.8, Django 3.1.5

Basically i don't use whitenoise. In my opinion Django's ManifestStaticFilesStorage class do the great job in collect staticfiles. It add unique hash, works fast and don't need any other dependencies.

On production serwer (where staticfiles are served by nginx from 'public' folder) my staticfiles settings looks like below:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'public', 'static')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

STATICFILES_STORAGE = "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"

and that's all. With this configuration after run python manage.py collectstatic Django will get all files from static folder and move it into public. It will also create copy of each file with unique id and will create staticfiles.json which contain all staticfiles map with original static files names and its "hashed" version, e.g.

{... "css/main.css": "css/main.31cdb680414e.css", main.js": "main.b08f762abac7.js"...}

With this approach if you use in your template: {% static 'images/my-image.jpg' %} it will be converted into path_to_image/my-image.828172380.jpg What is quite convinience especially if you change something in your css or js files and want to apply changes to all without necessity of clearing browser cache.

One important note You can face issue described here "Missing staticfiles manifest for... " if you will add staticfiles with slash at the beginning. So in your template:

This will work

<div class="slide slide1" style="background-image: url('{% static 'images/slider/129557309.jpg' %}');">

This is wrong and won't work, you will have exception

<div class="slide slide1" style="background-image: url('{% static '/images/slider/129557309.jpg' %}');">

It's small difference but you have to remember it. Hope it will be helpfull :)

Rejoin answered 19/2, 2021 at 14:6 Comment(1)
Support for STATICFILES_STORAGE was removed from Django 5.1Sup
S
2

An actual solution can be found here

The problem is that Django doesn't actually knows where the static file is and when you do
python manage.py collectstatic it actually doesn't copy it to you're STATIC_ROOT

Solution

  1. manually try to find you're static files using python manage.py findstatic <your static files>

    you need to do this for every static files. if this command didn't work that means Django can't find your static files. In order to fix that problem you need to define the static files path inside your
    STATIC_DIRS inside your settings.py

example

STATIC_DIRS = [
   os.path.join(BASE_DIR, "path/to/your/staticfiles")
]
  1. define
STATIC_ROOT = "staticfiles"
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
  1. do python manage.py collectstatic
    and after that it should be working
Shiksa answered 8/7, 2022 at 12:42 Comment(3)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Pierpont
This answer saved me. When I reverted my commit, my staticfiles/admin folder was removed somehow, and I had never manually removed it. This solved my problem.Schiro
Shouldn't the setting be STATICFILES_DIRS , not STATIC_DIRS ?Sup
M
1

I had the same issue and I fixed by changing STATICFILES_STORAGE to:

STATICFILES_STORAGE = 'cloudinary_storage.storage.StaticHashedCloudinaryStorage'

Then you should run:

python manage.py collectstatic
Montpellier answered 1/12, 2019 at 19:29 Comment(1)
Django 5.1 has removed support for STATICFILES_STORAGESup
J
1

i have this in my setting.py

DEBUG = False
try:
    from .local_setting import *
except ImportError:
    pass

after I removed the try block, everything work again.

Jarlathus answered 28/2, 2020 at 14:12 Comment(1)
Only DEBUG=False, related to this problem, if you set it to DEBUG=True, you'll achieve the same.Clementeclementi
D
1

This answer helped my solve this problem.

First add this in settings.py

LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
    'verbose': {
        'format': ('%(asctime)s [%(process)d] [%(levelname)s] '
                   'pathname=%(pathname)s lineno=%(lineno)s '
                   'funcname=%(funcName)s %(message)s'),
        'datefmt': '%Y-%m-%d %H:%M:%S'
    },
    'simple': {
        'format': '%(levelname)s %(message)s'
    }
},
'handlers': {
    'null': {
        'level': 'DEBUG',
        'class': 'logging.NullHandler',
    },
    'console': {
        'level': 'INFO',
        'class': 'logging.StreamHandler',
        'formatter': 'verbose'
    }
},
'loggers': {
    'django': {
        'handlers': ['console'],
        'level': 'DEBUG',
        'propagate': True,
    },
    'django.request': {
        'handlers': ['console'],
        'level': 'DEBUG',
        'propagate': False,
    },
}}

And then add this at end of your settings.py, if you are using django-heroku:

django_heroku.settings(config=locals(), staticfiles=False,logging=False)
Deputize answered 27/7, 2020 at 3:39 Comment(0)
I
1

In my case this was occurring because I am using nginx (Docker) to serve the static images. This is occurring when the app is running, not during testing like the OP.

The issue was that I was declaring the STATIC_ROOT to be outside of the Django app:

STATIC_ROOT = "../staticfiles"

I did this so that the assets were not unnecessarily copied onto the Django container. This works fine if you use the default STATICFILES_STORAGE, but when using ManifestStaticFilesStorage, the static file lookup fails because it cannot find manifest file asset in directory on the Django container.

The solution is simply to declare static files to be present on the Django image:

STATIC_ROOT = "staticfiles"

This does create to copies of the static files: one copy on the nginx container, and one on Django, but it means that the Django lookup logic succeeds, even though the files are served from nginx.

Another option would be to disable the manifest_strict attribute:

storage.ManifestStaticFilesStorage.manifest_strict

If a file isn’t found in the staticfiles.json manifest at runtime, a ValueError is raised. This behavior can be disabled by subclassing ManifestStaticFilesStorage and setting the manifest_strict attribute to False – nonexistent paths will remain unchanged.

Illustrational answered 8/10, 2020 at 11:1 Comment(0)
M
0

In my case, I am not sure why it happens, but after I removed the heroku settings django_heroku.settings(locals()) everything worked again.

Mickimickie answered 12/12, 2019 at 2:42 Comment(0)
U
0

For me, the issue was that, in one place, I was referring to a folder, images, as {% static 'images' %}. Changing that to {% static 'images/' %}, with a slash at the end, solved the problem. Mind-boggling.

Uria answered 5/11, 2022 at 19:36 Comment(0)
E
0

Here's an updated answer for those using Whitenoise along with Django >= 4.2:

# settings.py
STORAGES = {
    "default": {
        "BACKEND": "django.core.files.storage.FileSystemStorage",
    },
    "staticfiles": {
        "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage"
        if not TESTING
        else "whitenoise.storage.CompressedStaticFilesStorage",
        # disables the caching behavior for the tests, for static files to load properly
    },
}

This new STORAGES setting is mentioned in this release note and here in the Django documentation.

Whitenoise documentation has also been updated as you can see here.

Extractor answered 27/4, 2023 at 7:41 Comment(2)
It does not seem to work for me. I am running Django 4.2.6 and whitenoise 6.6.9Evensong
Still working well for me with the same config ;) Your issue probably stems from something else!Oversew
B
0

The issue might be that you ran collectstatic with different settings than when you run your server. In particular, if the value of STORAGES["staticfiles"]or STATICFILES_STORAGE (for older versions of Django) is set to a subclass of ManifestStaticFilesStorage when running, but was set to something different or not set when you ran collectstatic, you will get this error.

This can happen because you set the environment variable DJANGO_SETTINGS_MODULE to different values when you run collectstatic vs. your server, or it can happen if your settings module depends on other environment variables to determine its settings.

My solution was to define a settings module specifically for running collectstatic because I didn't want to use all the settings for my staging and production environments when I run collectstatic, but I do want to set the STORAGES setting.

Buttonhook answered 22/8 at 20:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.