Testing Templates Used in Views
A snippet showing how to test template rendering in Django views.
# models.py
from django.db import models
class Page(models.Model):
title = models.CharField(max_length=100)
# admin.py
from django.contrib import admin
from .models import Page
@admin.register(Page)
class PageAdmin(admin.ModelAdmin):
list_display = ("title",)
# Result (tests/test_templates.py)
from django.test import TestCase
from django.urls import reverse
class TemplateUseTest(TestCase):
def test_uses_template(self):
res = self.client.get(reverse("page-list"))
self.assertTemplateUsed(res, "pages/list.html")
Explanation:
-
Use
assertTemplateUsed(response, "template.html")to verify a specific template rendered. - Also assert context values if necessary.
- Category Testing
- Total Views 713
- Last Modified 30 March, 2026
- Tags #testing #templates #views #client
Previous snippet
Testing Redirects
Next snippet