lmi*_*asf 1 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'))
Run Code Online (Sandbox Code Playgroud)
我正在使用 Codeship for CI,当我运行时:
$ python manage.py collectstatic --noinput
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
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'
Run Code Online (Sandbox Code Playgroud)
我在本地没有问题,所以我想知道是什么导致了这个问题以及如何解决它。我从代码中了解到,我不能将该函数static用于模型字段。
有人知道如何解决这个问题吗?有人可以解释我为什么会这样吗?
您可以通过将static()调用移出模型字段并将默认值更改为 string来规避此问题并改进代码"pledges/images/no-profile-photo.png"。它应该是这样的:
avatar_url = models.URLField(default='pledges/images/no-profile-photo.png')
访问时avatar_url,请使用
(frontend / Django Templates option) {% static profile_instance.avatar_url %},其中profile_instance是引用 Profile 对象的上下文变量。
(后端/Python 选项)使用static(profile_instance.avatar_url).
通过使用static()默认值的结果,应用程序将一个包含STATIC_URL前缀的 URL 放入数据库中——这就像对其进行硬编码,因为数据不会更改settings.py。更一般地说,您根本不应该将 的结果存储static()在数据库中。
如果您确保每次访问时都使用{% static %}标记或static()函数avatar_url以在前端显示,STATIC_URL则仍将在运行时根据您的环境配置添加。
看起来你有一个循环依赖:
collectstatic 需要运行才能创建 manifest.json
您的应用程序需要加载才能运行manage.py命令,该命令调用static()
static()依赖入口manifest.json来解决。
| 归档时间: |
|
| 查看次数: |
5243 次 |
| 最近记录: |