检查django.test.TestCase中的模板名称
我正在尝试为我们的简单应用程序编写一个测试。尽管所有测试都在完美地运行,但我仍然希望检查视图渲染或重定向到的模板名称。检查视图渲染的模板的最佳方法是什么 可能是这样的:检查django.test.TestCase中的模板名称,django,templates,testing,Django,Templates,Testing,我正在尝试为我们的简单应用程序编写一个测试。尽管所有测试都在完美地运行,但我仍然希望检查视图渲染或重定向到的模板名称。检查视图渲染的模板的最佳方法是什么 可能是这样的: self.assertTrue('test.html' in self.templates) 或 如何实现这一点。来自: response=self.client.get(“/my/view/url”) 你能行 self.assertEqual(response.templates[0].name,“expected\u te
self.assertTrue('test.html' in self.templates)
或
如何实现这一点。来自:
response=self.client.get(“/my/view/url”)
你能行
self.assertEqual(response.templates[0].name,“expected\u template.html”)
或:
self.assertEqual(response.template[0].name,“预期的\u template.html”)
因为“模板”和“模板”是相同的数组。此数组列表的后续(非零)项包括或扩展了模板。来源:
response=self.client.get(“/my/view/url”)
你能行
self.assertEqual(response.templates[0].name,“expected\u template.html”)
或:
self.assertEqual(response.template[0].name,“预期的\u template.html”)
因为“模板”和“模板”是相同的数组。此数组列表的后续(非零)项包括或扩展模板。您应该使用
assertTemplateUsed
():
您应该使用
assertTemplateUsed
():
self.assertTrue(self.template.name, 'test.html')
response = self.client.get('/url/')
self.assertTemplateUsed(response, 'test.html')