Performance: TemplateView for simple pages
This affects the server response time and initial page load speed by simplifying view logic and reducing unnecessary processing.
Jump into concepts and practice - no test required
from django.views.generic import TemplateView class AboutView(TemplateView): template_name = 'about.html'
from django.http import HttpResponse def about(request): html = '''<html><body><h1>About Us</h1><p>Welcome to our site.</p></body></html>''' return HttpResponse(html)
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual HTML in view | N/A (server-side) | N/A | N/A | [X] Bad |
| TemplateView with cached template | N/A (server-side) | N/A | N/A | [OK] Good |
TemplateView?TemplateView is designed to render a template without extra logic, ideal for static pages.TemplateView just shows a template.TemplateView?template_name.from django.views.generic import TemplateView
class AboutPageView(TemplateView):
template_name = 'about.html'
Assuming about.html contains <h1>About Us</h1>, what will the browser show?TemplateView renders the specified template as HTML without needing extra context.about.html contains <h1>About Us</h1>, so this will be rendered as a heading.TemplateView code?
from django.views.generic import TemplateView
class ContactView(TemplateView):
template = 'contact.html'TemplateView is template_name.template, which Django does not recognize, causing the view to fail to find the template.TemplateView. Which of these is the best way to do it?TemplateView is designed to serve static templates easily by setting template_name.as_view() and sets the URL path, which is the standard pattern.