【问题标题】:Missing staticfiles manifest entry while rendering template in Django TestCases在 Django TestCases 中呈现模板时缺少静态文件清单条目
【发布时间】:2018-08-28 09:57:51
【问题描述】:

我在运行 TestCases 时遇到了问题,我在其中呈现页面模板以测试生成的 HTML 片段。

这是我正在运行的测试类型的示例:

test.py

from django.test import TestCase

class NavTestCase(TestCase):
    def test_standard_user_nav(self):
        self.client.login(username='username', password='password')
        user = auth.get_user(self.client)
        response = self.client.get('/')
        content = response.render().content
        # Run logic to check pieces of the nav in the rendered HTML

requirements.txt

django-material==1.0.0
django-nose==1.4.5
nose==1.3.7

settings.py

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

问题在于我添加到我的base.html 文件中的代码,以支持使用django-material 进行最近的网站重新设计。下面,<head> 中的三行是直接从django-material 文档中复制而来的。

base.html

{% load static %}
<html lang="en-us">
<head>
    {% include 'material/includes/material_css.html' %}
    <script src="{% static '[material/js/jquery.js' %}"></script>
    {% include 'material/includes/material_js.html' %}
</head>
<body>...</body>
</html>

作为参考,以下是这些文件的链接:

但是,这些行破坏了我的测试。我收到这两个错误:

  • WARNING Exception raised while rendering {% include %} ... ValueError: Missing staticfiles manifest entry for 'material/fonts/material-design-icons/material-icons.css' ==> 这是 material_css.html 中的第一个调用。
  • ValueError: Missing staticfiles manifest entry for 'material/js/jquery.js' ==> 这是 django-material 文档指定的 base.html 中的第二行。

我对任何其他静态文件都没有这个问题,无论是我创建的那些(我在上面粘贴的精简 base.html 中遗漏了其条目),甚至看起来是 @ 提供的其他静态文件987654338@. 实际上,仔细观察,当我删除这三行时,它会在随后对“静态”的调用中中断(为了简单起见,我再次从这个示例中省略了)。

现在,按照this StackOverflow question 的建议,当我在本地运行collectstatic 时,它确实解决了这个问题。但是,当我部署代码并远程运行测试时,我不知道如何让collectstatic 在那里运行。我尝试将测试的基类更改为StaticLiveServerTestCase,但这并没有什么不同。我还从Nose documentation 看到,我可以在我的测试类中创建一个setUp() 例程,但是鉴于我在我的代码中的许多测试类中运行这样的测试,我想避免多次运行collectstatic

有什么想法吗?

谢谢!

【问题讨论】:

    标签: django nose django-testing django-tests collectstatic


    【解决方案1】:

    显然这是一项功能,而不是错误。在运行测试之前,我必须更改部署运行collectstatic 的方式——这需要更改为我的持续集成环境定制的.yml 配置文件——与上述任何内容完全无关。哦。

    【讨论】:

      猜你喜欢
      • 2018-10-05
      • 2020-11-18
      • 2020-11-19
      • 2017-10-24
      • 2020-01-15
      • 2017-12-29
      • 2013-05-13
      • 2020-02-01
      • 2021-06-06
      相关资源
      最近更新 更多