Introduction
TemplateView helps you quickly show simple web pages without writing extra code. It just loads and displays a template.
Jump into concepts and practice - no test required
TemplateView helps you quickly show simple web pages without writing extra code. It just loads and displays a template.
from django.views.generic import TemplateView class YourPageView(TemplateView): template_name = 'your_template.html'
Set template_name to the HTML file you want to show.
This class automatically renders the template when the page is requested.
home.html template when the home page is visited.from django.views.generic import TemplateView class HomePageView(TemplateView): template_name = 'home.html'
about.html template.from django.views.generic import TemplateView class AboutPageView(TemplateView): template_name = 'about.html'
This example creates a Contact page URL that shows the contact.html template when visited.
from django.urls import path from django.views.generic import TemplateView class ContactPageView(TemplateView): template_name = 'contact.html' urlpatterns = [ path('contact/', ContactPageView.as_view(), name='contact'), ]
TemplateView is best for pages without special data or logic.
For pages needing data, use other views like ListView or DetailView.
Remember to create the template file in your templates folder.
TemplateView quickly shows simple pages by loading a template.
Set template_name to choose which HTML file to display.
Use it for static pages like About, Contact, or Terms.
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.