Django 模型:ValueError:缺少“file_name.ext"的静态文件清单条目; [英] Django Model: ValueError: Missing staticfiles manifest entry for "file_name.ext"

查看:20
本文介绍了Django 模型:ValueError:缺少“file_name.ext"的静态文件清单条目;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在您将其标记为重复之前,我已阅读ValueError: Missingfavicon.ico"的静态文件清单条目,它不能解决我的问题.

我有以下模型:

from django.contrib.staticfiles.templatetags.staticfiles import static类配置文件(模型.模型):user = models.ForeignKey(SocialUser, on_delete=models.PROTECT)avatar_url = 模型.URLField(默认=静态('承诺/图像/无配置文件照片.png'))

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

$ python manage.py collectstatic --noinput

我收到以下错误:

回溯(最近一次调用最后一次): 中的文件manage.py",第 22 行execute_from_command_line(sys.argv)文件/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py",第364行,在execute_from_command_line实用程序.execute()文件/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/core/management/__init__.py",第338行,在执行django.setup()文件/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/__init__.py",第 27 行,在设置中apps.populate(设置.INSTALLED_APPS)文件/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/apps/registry.py",第108行,填充app_config.import_models()文件/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/apps/config.py",第 202 行,在 import_modelsself.models_module = import_module(models_module_name)文件/home/rof/.pyenv/versions/3.6/lib/python3.6/importlib/__init__.py",第 126 行,在 import_module 中返回_bootstrap._gcd_import(名称[级别:],包,级别)文件",第 994 行,在 _gcd_import 中文件",第 971 行,在 _find_and_load 中文件",第 955 行,在 _find_and_load_unlocked文件",第 665 行,在 _load_unlocked文件<frozen importlib._bootstrap_external>",第 678 行,在 exec_module 中_call_with_frames_removed 中的文件<frozen importlib._bootstrap>",第 219 行文件/home/rof/src/github.com/company-name/project-name/pledges/models.py",第106行,在<module>类配置文件(模型.模型):文件/home/rof/src/github.com/company-name/project-name/pledges/models.py",第 109 行,在 Profile 中默认=静态('承诺/图像/无配置文件照片.png'))文件/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/contrib/staticfiles/templatetags/staticfiles.py",第12行,静态返回_静态(路径)文件/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/templatetags/static.py",第166行,静态返回 StaticNode.handle_simple(path)文件/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/templatetags/static.py",第117行,handle_simple返回 staticfiles_storage.url(path)文件/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py",第 162 行,在 url 中返回 self._url(self.stored_name, name, force)文件/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py",第 141 行,在 _urlhashed_name = hashed_name_func(*args)文件/home/rof/.pyenv/versions/3.6/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py",第432行,在stored_name中raise ValueError("缺少 '%s' 的静态文件清单条目" % clean_name)ValueError:缺少承诺/图像/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() 的结果作为默认值,应用程序将一个包含 STATIC_URL 前缀的 URL 放入数据库中——这就像很难- 编码它,因为当 settings.py 发生时数据不会改变.更一般地说,您根本不应该将 static() 的结果存储在数据库中.

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

这个 SO 线程有很多关于静态文件的好内容

为什么会发生错误:

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

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

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

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

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?

解决方案

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.

这篇关于Django 模型:ValueError:缺少“file_name.ext"的静态文件清单条目;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆