heroku Django ValueError:缺少staticfiles清单条目,但清单显示为显示条目

vecaoik1  于 2023-06-23  发布在  Go
关注(0)|答案(4)|浏览(101)

在部署到Heroku的Django 1.11应用上。当加载根URL /时(我假设Django在homepage.html模板中到达{% static 'angular/angular.min.js' %}时),我得到以下错误:

ValueError: Missing staticfiles manifest entry for 'angular/angular.min.js'
  File "django/core/handlers/exception.py", line 41, in inner
    response = get_response(request)
  File "django/core/handlers/base.py", line 187, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "django/core/handlers/base.py", line 185, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "homepage/views.py", line 87, in homepage
    "latest_python3": Version.objects.filter(supports_python3=True).select_related("package").distinct().order_by("-created")[0:5]
  File "django/shortcuts.py", line 30, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "django/template/loader.py", line 68, in render_to_string
    return template.render(context, request)
  File "django/template/backends/django.py", line 66, in render
    return self.template.render(context)
  File "django/template/base.py", line 207, in render
    return self._render(context)
  File "newrelic/api/function_trace.py", line 60, in dynamic_wrapper
    return wrapped(*args, **kwargs)
  File "django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "django/template/loader_tags.py", line 177, in render
    return compiled_parent._render(context)
  File "newrelic/api/function_trace.py", line 60, in dynamic_wrapper
    return wrapped(*args, **kwargs)
  File "django/template/base.py", line 199, in _render
    return self.nodelist.render(context)
  File "django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "django/template/defaulttags.py", line 411, in render
    return strip_spaces_between_tags(self.nodelist.render(context).strip())
  File "django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "newrelic/hooks/framework_django.py", line 765, in wrapper
    return wrapped(*args, **kwargs)
  File "django/template/loader_tags.py", line 72, in render
    result = block.nodelist.render(context)
  File "django/template/base.py", line 990, in render
    bit = node.render_annotated(context)
  File "django/template/base.py", line 957, in render_annotated
    return self.render(context)
  File "django/templatetags/static.py", line 105, in render
    url = self.url(context)
  File "django/templatetags/static.py", line 102, in url
    return self.handle_simple(path)
  File "django/templatetags/static.py", line 117, in handle_simple
    return staticfiles_storage.url(path)
  File "django/contrib/staticfiles/storage.py", line 162, in url
    return self._url(self.stored_name, name, force)
  File "django/contrib/staticfiles/storage.py", line 141, in _url
    hashed_name = hashed_name_func(*args)
  File "django/contrib/staticfiles/storage.py", line 432, in stored_name
    raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)

manifest里有什么?

我对Django文档的理解是,清单是一个名为staticfiles.json的文件。该文件似乎包含正确的条目(出于格式原因,我删除了所有不相关的条目):

$ heroku run cat ./staticfiles/staticfiles.json
Running cat ./staticfiles/staticfiles.json
{"paths": {"angular/angular.min.js": "angular/angular.min.df1c56732ca5.js", "angular/controllers.js": "angular/controllers.af8e9f9a2645.js"}, "version": "1.0"}

相关设置:

Heroku's instructions for service Static Files with Django

# in requirements.txt
whitenoise

# in settings/base.py
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATIC_URL = "/static/"
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    # ...others here too
]

django_heroku.settings(locals())

(更新:STATICFILES_STORAGE)

基于下面的建议之一,我删除了STATICFILES_STORAGE的值。django-heroku将STATICFILES_STORAGE的值设置为whitenoise.storage.CompressedManifestStaticFilesStorage。我通过在www.example.com文件的末尾添加del STATICFILES_STORAGE来删除它settings.py。Django然后将值重置为默认值django.contrib.staticfiles.storage.StaticFilesStorage。结果是所有静态文件要么404要么在浏览器中得到MIME类型错误。

vu8f3i0k

vu8f3i0k1#

您是否手动创建了staticfiles目录,然后在部署之前运行了django-admin collectstatic
这是关于这个的文档。

x6h2sr28

x6h2sr282#

**更新:**此问题已修复。不幸的是,这个解决方案不会对其他人有用。对此我深表歉意。问题是我的设置被另一个正在导入的模块中的设置覆盖了。我在上面的问题中列出的设置实际上在Heroku上起作用。对不起大家,感谢那些帮助我调试这个的人。

gcuhipw9

gcuhipw93#

提示任何登陆这里的人

我也有同样的问题,在我的情况下,我不得不删除开头的斜线
{% static '/admin/js/vendor/jquery/jquery.js' %}更改为{% static 'admin/js/vendor/jquery/jquery.js' %}解决了我的问题。
我知道这与这个确切的问题没有直接关系,但是因为我带着我的问题来到这里,我想我会把它贴在这里,希望这能帮助将来来到这里的人。

ecfsfe2w

ecfsfe2w4#

ValueError是由Django staticfiles应用程序引发的,但在您的情况下,它实际上来自WhiteNoise,确切地说是来自whitenoise.storage.CompressedManifestStaticFilesStorage。该主题在Django上的WhiteNoise文档的“Additional Notes”部分进行了讨论。
仅供参考,我得到了相同的错误,例如,当我在Django Restframework端点上使用Pytest运行测试时(使用format=api请求上下文)。
WhiteNoise文档建议使用findstatic分析问题,如果没有其他帮助,则通过Django项目设置中的WHITENOISE_MANIFEST_STRICT = False消 debugging 误。
这可能归结为在任何事情之前(甚至在运行测试之前)运行collectstatic,例如

python manage.py collectstatic --noinput

**P.S.:**您不应该 * 将del STATICFILES_STORAGE添加到设置模块的末尾。这将破坏使用WhiteNoise进行静态文件优化的目的。

相关问题