0
0
Djangoframework~5 mins

TemplateView for simple pages in Django

Choose your learning style9 modes available
Introduction

TemplateView helps you quickly show simple web pages without writing extra code. It just loads and displays a template.

You want to show a static About Us page.
You need a simple Contact page without special logic.
You want to display a Terms and Conditions page.
You need a quick landing page for your site.
You want to serve a simple FAQ page.
Syntax
Django
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.

Examples
This shows the home.html template when the home page is visited.
Django
from django.views.generic import TemplateView

class HomePageView(TemplateView):
    template_name = 'home.html'
This displays a simple About page using the about.html template.
Django
from django.views.generic import TemplateView

class AboutPageView(TemplateView):
    template_name = 'about.html'
Sample Program

This example creates a Contact page URL that shows the contact.html template when visited.

Django
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'),
]
OutputSuccess
Important Notes

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.

Summary

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.