Welcome to Django class-based views. After watching this video, you will be able to: Create class-based and generic class-based views, and Compare the pros and cons of function-based views and class-based views. The function-based view was the original view implementation in Django. It was created to solve a specific problem with a request parameter and explicit logic for how to generate a response. The class-based view was added to Django to improve the extensibility and reusability of views. Whether a view is built using function-based views or class-based views, a view is essentially a Python function or callable. The main issue with the function-based view is it is normally very hard to extend or customize. To address these issues, the class-based view was created. Let’s see how to build a class-based view to return an HTTP Response. Instead of creating a function, you define a class subclassing the Django View base class. Then, you can access some common methods abstracted by the View base class, such as Get or Post to handle an HTTP Get or Post request. Next, implement your own logic to handle http requests. For example, to handle the HTTP Get method from a URL, we implement a simple Get method by first querying the course record from the database, inserting the course name into an html template, and populating HttpResponse. We also need to map a URL pattern to a class-based view class. This is like configuring the URL for the function-based view. The only difference is that for the view argument you need to specify the as_view function or the CourseView class we defined. The as_view function is inherited from the View base class. The Django View class is the base class for all class-based views. The Django View class defines an as_view method, which also defines an inner_view method and will be returned as a callable. So, when we add them to the URLconf using the view.as_view class method, it returns a function. After being called, the view callable passes the request to the dispatch method, which will execute the http method accordingly, such as GET or POST. So, even a class-based view is essentially a function to handle a certain http method. Thus, both function-based and class-based views are functions. In web app development, we need to perform common tasks such as displaying a list or showing the details of an object in drill-down. To speed up development and solve those common tasks, Django provides some built-in view classes called generic-based views for developers to reuse and extend with minimal code changes. Let’s see an example of a generic class-based view to display a course detail like we have done before. First, we need to define a CourseView that extends the generic DetailView. Then we specify the model to be Course and provide an html template file path. The methods provided by the DetailView know how to get the object and populate the html page. So, developers only need to write two lines of code to display a course object in a browser based on an html template, without writing any code to handle the http request. Let’s look at a few examples of generic class-based views. ListView represents a list of objects. DetailView represents the details of an object. FormView represents a form, and A group of Date views handles date-based data. So far, you’ve learned about the function-based, class-based, and generic class-based views. Let’s summarize the pros and cons to help you decide which one to use in your application. For the function-based view, the main pros are that it’s simple to write code and easy to understand. You can explicitly write your logic without following any extra structure or designs such as the methods defined in View base class. If you have a very specific function that’s not likely to be reused, the function-based view could be your choice. The main cons are that function-based views are difficult to extend or reuse. And they handle http request methods using conditional statements, which may increase the code complexity. For Class-based views, the pros include reusability and extendibility. Also, you can handle requests using class methods. And you can leverage the generic class-based views to solve common tasks. The main cons for class-based views are that adding the extra View base class inheritance makes the code harder to read. Implicit code is hidden, and developers must check the source code to understand exactly how the views are implemented. In this video, you learned: How to implement class-based views. How to use Django built-in generic class views to solve common tasks. The pros and cons of function-based and class-based views.