class-based-views Flashcards
What was the original view implementation in Django?
The function-based view was the original view implementation in Django.
What is the purpose of a function-based view?
A function-based view was created to solve a specific problem with a request parameter and explicit logic for generating a response.
Why were class-based views added to Django?
Class-based views were added to Django to improve the extensibility and reusability of views.
What is the main issue with function-based views?
The main issue with function-based views is that they are normally very hard to extend or customize.
What do you subclass to create a class-based view?
To create a class-based view, you subclass the Django View base class.
Which methods can you access by subclassing the Django View base class?
By subclassing the Django View base class, you can access methods like Get or Post to handle HTTP Get or Post requests.
What function must you call to map a URL pattern to a class-based view?
To map a URL pattern to a class-based view, you call the as_view function.
What does the as_view method in the Django View class return?
The as_view method in the Django View class returns a callable function.
What method does the view callable pass the request to?
The view callable passes the request to the dispatch method.
What is the purpose of Django’s generic class-based views?
Django’s generic class-based views are built-in view classes that handle common tasks such as displaying a list or showing object details with minimal code changes.
What does the DetailView generic class-based view represent?
The DetailView generic class-based view represents the details of an object.
What are the pros of using function-based views?
The pros of using function-based views are that they are simple to write and easy to understand.
What are the cons of using function-based views?
The cons of using function-based views are that they are difficult to extend or reuse, and they handle HTTP request methods using conditional statements, increasing code complexity.
What are the pros of using class-based views?
The pros of using class-based views are reusability, extendibility, and the ability to handle requests using class methods.
What are the cons of using class-based views?
The cons of using class-based views are that the extra View base class inheritance makes the code harder to read, and implicit code is hidden, requiring developers to check the source code to understand the views.