标签:django-staticfiles python django django-models
在将其标记为重复之前,我已阅读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用于模型字段.
有人知道怎么弄清楚这个吗?有人可以解释一下为什么会这样吗?
解决方法:
解:
您可以通过将static()调用移出模型字段并将默认值更改为字符串“pledges / images / no-profile-photo.png”来避免此问题并改进代码.它应该如下所示:
avatar_url = models.URLField(默认=’pledges / images / no-profile-photo.png’)
当您访问avatar_url时,请使用其中之一
>(前端/ Django模板选项){%static profile_instance.avatar_url%},其中profile_instance是引用Profile对象的上下文变量.
>(backend / Python选项)使用static(profile_instance.avatar_url).
说明:
通过使用static()的结果作为默认值,应用程序在数据库中放置一个包含STATIC_URL前缀的URL – 这就像硬编码一样,因为在settings.py时数据不会改变.更一般地说,您根本不应将static()的结果存储在数据库中.
如果您确保每次访问avatar_url以在前端显示时使用{%static%}标记或static()函数,则仍会在运行时根据您的环境配置添加STATIC_URL.
This SO thread has a lot of good content on staticfiles
为什么会发生错误:
看起来你有一个循环依赖:
> collectstatic需要运行才能创建manifest.json
>您的应用程序需要加载才能运行manage.py命令,该命令调用static()
> static()依赖manifest.json中的条目来解析.
标签:django-staticfiles,python,django,django-models 来源: https://codeday.me/bug/20191003/1849331.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。