Django Model: ValueError: Missing staticfiles manifest entry for "file_name.ext"

Django Model: ValueError: Missing staticfiles manifest entry for "file_name.ext"

在您将其标记为重复之前,我已阅读 ValueError:缺少 'favicon.ico' 的静态文件清单条目 ,并没有解决我的问题。

我有以下型号:

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

我正在为 CI 使用 Codeship,当我 运行:

$ python manage.py collectstatic --noinput

我收到以下错误:

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'

我在本地没有问题,所以我想知道是什么导致了这个问题以及如何解决它。我从代码中了解到,我不能将函数 static 用于模型字段。

有人知道如何解决这个问题吗?有人可以向我解释为什么会这样吗?

解决方案:

您可以通过将 static() 调用移出模型字段并将默认值更改为字符串 "pledges/images/no-profile-photo.png" 来规避此问题并改进代码。它应该看起来像这样:

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

当您访问 avatar_url 时,请使用

  1. (前端/Django 模板选项){% static profile_instance.avatar_url %},其中 profile_instance 是引用 Profile 对象的上下文变量。

  2. (后端/Python选项)使用static(profile_instance.avatar_url).

解释:

通过使用 static() 的结果作为默认值,应用程序将 URL 放入包含 STATIC_URL 前缀的数据库中——这类似于硬编码这是因为当 settings.py 改变时数据不会改变。更一般地说,您根本不应该将 static() 的结果存储在数据库中。

如果您确保每次访问 avatar_url 以在前端显示时都使用 {% static %} 标记或 static() 函数,STATIC_URL 将仍会根据您在 运行 时的环境配置添加。

为什么会发生错误:

看起来你有一个循环依赖:

  1. collectstatic 需要 运行 才能创建 manifest.json

  2. 您的应用程序需要加载才能执行 运行 manage.py 命令,该命令调用 static()

  3. static() 依赖 manifest.json 中的条目来解析。