Class-Based Views
As the name implies, class-based views are implemented as Python classes. Using the principles of class inheritance, these classes are implemented as subclasses of Django's generic view classes. Unlike function-based views, where all the view logic is expressed explicitly in a function, Django's generic view classes come with various pre-built properties and methods that can provide shortcuts to writing clean, reusable views. This property comes in handy quite often during web development; for example, developers often need to render an HTML page without needing any data inserted from the database, or any customization specific to the user. In this case, it is possible to simply inherit from Django's TemplateView
, and specify the path of the HTML file. The following is an example of a class-based view that can display the same message as in the function-based view example:
from django.views.generic import TemplateView class HomePage(TemplateView...