[Django]-Check template name in django.test.TestCase

4👍

You should use assertTemplateUsed (docs):

response = self.client.get('/url/')
self.assertTemplateUsed(response, 'test.html')

1👍

From:
response = self.client.get("/my/view/url")

you can do

self.assertEqual(response.templates[0].name, "expected_template.html")

or:

self.assertEqual(response.template[0].name, "expected_template.html")

as ‘template’ and ‘templates’ are the same array. Subsequent (non-zero) entries of this array list included or extended templates.

Leave a comment