Django Model:ValueError:缺少staticfiles清单“file_name.ext”的条目

时间:2018-04-24 18:59:48

标签: python django django-models django-staticfiles

在您将其标记为重复之前,我已阅读ValueError: Missing staticfiles manifest entry for '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'))

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

$ 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用于模型字段。

有人知道怎么弄清楚这个吗?有人可以解释一下为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

解决方案:

您可以通过将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)

  3. 说明:

    通过使用static()的结果作为默认值,应用程序在数据库中放置一个包含STATIC_URL前缀的URL - 这就像硬编码一样,因为数据不会在settings.py时更改。更一般地说,您不应该将static()的结果存储在数据库中。

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

    This SO thread has a lot of good content on staticfiles

    为什么会发生错误:

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

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

    2. 您的应用程序需要加载才能运行调用manage.py

    3. static()命令
    4. static()依赖于manifest.json中的条目来解决。