108👍
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://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
53👍
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.
- [Django]-When should I use a custom Manager versus a custom QuerySet in Django?
- [Django]-Ignoring Django Migrations in pyproject.toml file for Black formatter
- [Django]-Naming convention for Django URL, templates, models and views
38👍
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
- [Django]-Multiple Models in a single django ModelForm?
- [Django]-Django multiple template inheritance – is this the right style?
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions
32👍
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'
- [Django]-Django model blank=False does not work?
- [Django]-Django Passing Custom Form Parameters to Formset
- [Django]-AngularJS with Django – Conflicting template tags
9👍
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'
)
- [Django]-Add additional options to Django form select widget
- [Django]-Phpmyadmin logs out after 1440 secs
- [Django]-Django 1.5b1: executing django-admin.py causes "No module named settings" error
6👍
In the latest version of Whitenoise (currently 5.2) you can now do..
WHITENOISE_MANIFEST_STRICT = False
- [Django]-Cross domain at axios
- [Django]-Delete multiple objects in django
- [Django]-New url format in Django 1.9
5👍
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’ %}
- [Django]-Overriding AppConfig.ready()
- [Django]-Django-allauth social account connect to existing account on login
- [Django]-Django's self.client.login(…) does not work in unit tests
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
-
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")
]
- define
STATIC_ROOT = "staticfiles"
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
- do
python manage.py collectstatic
and after that it should be working
- [Django]-What is the SQL ''LIKE" equivalent on Django ORM queries?
- [Django]-Disable session creation in Django
- [Django]-How to print BASE_DIR from settings.py from django app in terminal?
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
- [Django]-How to reset db in Django? I get a command 'reset' not found error
- [Django]-Django DateField default options
- [Django]-How do I filter ForeignKey choices in a Django ModelForm?
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.
- [Django]-How to check whether the user is anonymous or not in Django?
- [Django]-How do I go straight to template, in Django's urls.py?
- [Django]-Django rest framework serializing many to many field
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)
- [Django]-Execute code when Django starts ONCE only?
- [Django]-Django vs. Model View Controller
- [Django]-Django-Forms with json fields
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.
- [Django]-Foreign key from one app into another in Django
- [Django]-Using Cloudfront with Django S3Boto
- [Django]-How to test auto_now_add in django
1👍
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 🙂
- [Django]-What is the use of PYTHONUNBUFFERED in docker file?
- [Django]-Filtering dropdown values in django admin
- [Django]-Django unit tests without a db
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.
- [Django]-Select between two dates with Django
- [Django]-Django request get parameters
- [Django]-How to completely uninstall a Django app?
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.
- [Django]-Split models.py into several files
- [Django]-How do I perform query filtering in django templates
- [Django]-Django count RawQuerySet
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.
- [Django]-<Django object > is not JSON serializable
- [Django]-Django rest framework lookup_field through OneToOneField
- [Django]-Django query get last n records