2

Before you mark it as duplicate, I have read ValueError: Missing staticfiles manifest entry for 'favicon.ico' , and it does not solve my problem.

I have the following model:

from django.contrib.staticfiles.templatetags.staticfiles import static

class Profile(models.Model):
    user = models.ForeignKey(SocialUser, on_delete=models.PROTECT)
    avatar_url = models.URLField(
        default=static('pledges/images/no-profile-photo.png'))

I am using Codeship for CI, and when I run:

$ python manage.py collectstatic --noinput

I am getting the following error:

Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py", line 338, in execute
django.setup()
File "/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/home/rof/.pyenv/versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/rof/src/github.com/company-name/project-name/pledges/models.py", line 106, in <module>
class Profile(models.Model):
File "/home/rof/src/github.com/company-name/project-name/pledges/models.py", line 109, in Profile
default=static('pledges/images/no-profile-photo.png'))
File "/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/contrib/staticfiles/templatetags/staticfiles.py", line 12, in static
return _static(path)
File "/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/templatetags/static.py", line 166, in static
return StaticNode.handle_simple(path)
File "/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/templatetags/static.py", line 117, in handle_simple
return staticfiles_storage.url(path)
File "/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 162, in url
return self._url(self.stored_name, name, force)
File "/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 141, in _url
hashed_name = hashed_name_func(*args)
File "/home/rof/.pyenv/versions/3.6/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 'pledges/images/no-profile-photo.png'

I am not having issues locally, so I wonder what is causing this problem and how to solve it. What I understand from the code is that I cannot use the function static for a model field.

Does somebody have any idea how to figure out this? Can somebody explain me why this is happening?

lmiguelvargasf
  • 63,191
  • 45
  • 217
  • 228

1 Answers1

6

Solution:

You can circumvent this issue and improve the code by moving the static() call out of the model field and changing the default value to the string "pledges/images/no-profile-photo.png". It should look like this:

avatar_url = models.URLField(default='pledges/images/no-profile-photo.png')

When you access avatar_url, use either

  1. (frontend / Django Templates option) {% static profile_instance.avatar_url %}, where profile_instance is a context variable referring to a Profile object.

  2. (backend / Python option) use static(profile_instance.avatar_url).

Explanation:

By using the result of static() for a default value, the app is putting a URL in the database that includes the STATIC_URL prefix -- which is like hard-coding it because data won't change when settings.py does. More generally, you shouldn't store the results of static() in the database at all.

If you ensure that you're using the {% static %} tag or static() function each time you're accessing avatar_url for display on the frontend, STATIC_URL will still be added based on your environment config at runtime.

This SO thread has a lot of good content on staticfiles

Why the error is happening:

It looks like you have a circular dependency:

  1. collectstatic needs to run in order to create manifest.json

  2. your application needs to load in order to run manage.py commands, which calls static()

  3. static() relies on an entry in manifest.json to resolve.

Community
  • 1
  • 1
whp
  • 1,406
  • 10
  • 10
  • `static("pledges/images/no-profile-photo.png")` produces `"/static/pledges/images/no-profile-photo.png"`, so I don't think both produce the same. I have a variable in settings called `settings.STATIC_URL` which has the value of `'/static/'`, so I have tried `settings.STATIC_URL + 'pledges/images/no-profile-photo.png'` instead, but this solution is not so good because settings are configured by the site, and apps belong to a side, and models belongs to app. Therefore, it makes apps not reusable. – lmiguelvargasf Apr 24 '18 at 21:31
  • Your model field should be: `avatar_url = models.URLField(default='pledges/images/no-profile-photo.png')` – whp Apr 24 '18 at 21:40
  • the problem is that you are not considering the static files. Check [this](https://docs.djangoproject.com/en/2.0/howto/static-files/) – lmiguelvargasf Apr 24 '18 at 21:45
  • Please be more specific -- what do you mean by "not considering the static files?" It's hard to help without knowing what you are trying that isn't working. I'm very familiar with the staticfiles docs – whp Apr 24 '18 at 22:01
  • that you are keeping in mind somebody that wants to manage his static files. Let's say somebody who wants to have `STATIC_URL` as `/static/`. – lmiguelvargasf Apr 24 '18 at 22:04
  • Yes: when you _access_ `avatar_url` use either 1. (frontend / Django Templates option) `{% static profile_instance.avatar_url %}`, where `profile_instance` would be a context variable referring to a `Profile` object. Or 2. (backend / Python option) use `static(profile_instance.avatar_url)` – whp Apr 24 '18 at 22:11
  • that is a really nice answer. Thank so much. Would you mind adding that last part to the answer? – lmiguelvargasf Apr 24 '18 at 22:14