How Django Works (5) Class Based View
Why Class-based Views As is discussed in series 4 , with URL resolution, Django maps an URL to a given view function based on regular expression. So theoretically, function views would work just fine, because we have all the information we need to process a request, as is shown in the example below. from django.http import HttpResponse import datetime def current_datetime (request): now = datetime.datetime.now() html = "<html><body>It is now %s .</body></html>" % now return HttpResponse(html) So Why Bother With Class-based Views I think there are two major reasons: 1) to organize code related to specific HTTP methods (GET, POST, etc), so they can be addressed by separate methods instead of conditional branching in a big function; 2) to use mix-in, inheritance, and built-in views to reduce the boilerplate code. How Class-based Views Work Everything starts with base class View (site-packages\django\views\generic...