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
from django.views.generic import TemplateView

2. Set

from .views import * 
urlpatterns = [

3. Set index.html

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