How to Create and Use TemplateView in Django?

Published On: 22/09/2022 | Category: Django


Hi Dev,

I will explain step by step tutorial how to use templateview in django. This post will give you simple example of django templateview example. step by step explain how to create and use templateview in django with example. I explained simply step by step django templateview class based views example.

Django Class based views are simpler and efficient to manage than function-based views. A function based view with tons of lines of code can be converted into a class based views comes with few lines only.

So, in this example, TemplateView refers to a view just be a template. to define a template_name in your template view.

let's see bellow example here you will learn how to use templateview in django.

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example

Step 2: Create a App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

python3 manage.py startapp core

Step 3: Update setting.py

Next, then update INSTALLED_APPS within our settings.py file to notify Django about the app.

Next, you need to add it in the settings.py file as follows:

settings.py
....
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'core',
]

Step 4: Creating the Views

In this step, we need to configure our views. The AboutUs page will template and define, open the views.py file and add:

core/models.py
from django.shortcuts import render
from django.views.generic.base import TemplateView

class AboutUs(TemplateView):
    template_name = "core/about.html"

Step 5: Creating the Template

Next, then with your text editor create new templates files: core/about.html file and the add:

core/about.html
<div class="container" style="margin-top: 100px !important;">
    <h1 style="text-align:center;">Welcome to Tuts-Station.com</h1>
</div>

Step 6: Creating URLs

In this section, we need a urls.py file within the core app however Django doesn't create one for us with the startapp command. Create core/urls.py with your text editor and within this file we'll import yet-to-be-created function for each--AboutUs Note as well that we set an optional URL name for each.

Here's what it looks like:

core/urls.py
from django.urls import path
from .views import AboutUs

urlpatterns = [
    path('about/', AboutUs.as_view()),
]

Next, we require to add a URL path for our example app which can be done by importing include and setting a path for it.

example/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('core.urls')),
]

Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python manage.py runserver

Next, go to the http://localhost:8000/about/ address with a web browser.

I Hope It will help you....

Happy Pythonic Coding!