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

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

问题描述

在将其标记为重复项之前,我已阅读 ValueError:丢失staticfiles会显示 favicon.ico
的条目,它不能解决我的问题。

Before you mark it as duplicate, I have read ValueError: Missing staticfiles manifest entry for 'favicon.ico' , and it does not solve my problem.

我有以下模型:

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,并且在运行时:

I am using Codeship for CI, and when I run:

$ 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

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?

推荐答案

解决方案:


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

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

访问 avatar_url 时,请使用其中一个

When you access avatar_url, use either


  1. (前端/ Django模板选项) {%static profile_instance.avatar_url%} ,其中 profile_instance

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

(backend / Python option) use static(profile_instance.avatar_url).


说明:


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

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.

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

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.

此SO线程在静态文件上有很多不错的内容

您似乎具有循环依赖项:

It looks like you have a circular dependency:


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

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

your application needs to load in order to run manage.py commands, which calls static()

static()依赖于 manifest.json 中的一个条目解决。

static() relies on an entry in manifest.json to resolve.

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

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