How to write Class Based Views with DRY(Don’t repeat yourself) code using Inheritance.

Django has many rich amounts of features that it provides at its best. One of these is Class-Based Views. We’ll be going through a basic example of how to use it for the “DRY” code. In the end, you’ll save a lot of redundant code in your views file.

Requirements: Must be having a simple Django project setup in local.

Prerequisites: We will be using Template View in this example.

  1. Set views.py
from django.views.generic import TemplateView

2. Set urls.py

from .views import * 
urlpatterns = [

3. Set index.html

<!doctype html>                       
<html>
<head>
<title>{{ base_context_variable }}</title>
</head>
<body>
<h2> {{ index_variables }} </h2>
</body>
</html>