Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Function-based vs Class-based Views Decision in Django
📖 Scenario: You are building a simple Django web app to show a list of books. You want to learn how to create views using both function-based and class-based approaches. This will help you decide which style to use in different situations.
🎯 Goal: Create a Django view that shows a list of books using a function-based view first, then add a class-based view version. Finally, configure the URL patterns to use the class-based view.
📋 What You'll Learn
Create a list of books as a Python list of dictionaries
Write a function-based view called book_list_function that returns an HTTP response with the book titles
Write a class-based view called BookListClass inheriting from django.views.View that returns the same response
Configure the URL pattern to use the class-based view BookListClass.as_view()
💡 Why This Matters
🌍 Real World
Web developers often choose between function-based and class-based views in Django to organize their code and handle HTTP requests efficiently.
💼 Career
Understanding both view types is essential for Django developers to maintain and extend web applications professionally.
Progress0 / 4 steps
1
Create the initial data list of books
Create a Python list called books with these exact dictionaries: {'title': 'Django for Beginners'}, {'title': 'Python Crash Course'}, and {'title': 'Two Scoops of Django'}.
Django
Hint
Use square brackets to create a list. Each book is a dictionary with a 'title' key.
2
Write a function-based view to display book titles
Write a function called book_list_function that takes a request parameter and returns an HttpResponse with the book titles joined by commas. Use from django.http import HttpResponse.
Django
Hint
Use a generator expression inside join() to get all titles from the books list.
3
Write a class-based view to display book titles
Write a class called BookListClass that inherits from django.views.View. Add a get method that takes self and request and returns an HttpResponse with the book titles joined by commas.
Django
Hint
Remember to import View from django.views. The get method handles GET requests.
4
Configure URL pattern to use the class-based view
In the urlpatterns list, add a path with the URL string '' (empty string) that uses BookListClass.as_view() as the view. Import path from django.urls.
Django
Hint
Use path with an empty string for the root URL and call as_view() on the class-based view.
Practice
(1/5)
1. Which of the following is a key advantage of using class-based views (CBVs) over function-based views (FBVs) in Django?
easy
A. FBVs require less code for complex views.
B. CBVs are always faster than FBVs.
C. FBVs cannot handle POST requests.
D. CBVs allow reuse of common functionality through inheritance.
Solution
Step 1: Understand CBVs and inheritance
Class-based views use classes, so they can inherit and reuse code easily.
Step 2: Compare with FBVs
Function-based views are simple functions and do not support inheritance for reuse.
Final Answer:
CBVs allow reuse of common functionality through inheritance. -> Option D
Quick Check:
CBVs = reuse by inheritance [OK]
Hint: CBVs use classes, so they support inheritance and reuse [OK]
Common Mistakes:
Thinking CBVs are always faster
Believing FBVs can't handle POST
Assuming FBVs are better for complex views
2. Which of the following is the correct way to define a simple function-based view in Django?
easy
A. def my_view(request): return HttpResponse('Hello')
B. class my_view(View): return HttpResponse('Hello')
C. def my_view(): return HttpResponse('Hello')
D. class my_view: def get(): return HttpResponse('Hello')
Solution
Step 1: Check function signature for FBV
A function-based view must accept a request parameter.
Step 2: Validate return statement
The function should return an HttpResponse object.
Final Answer:
def my_view(request): return HttpResponse('Hello') -> Option A
Quick Check:
FBV needs request param and returns HttpResponse [OK]
Hint: FBVs are functions with request parameter returning HttpResponse [OK]
Common Mistakes:
Omitting the request parameter
Using class syntax for FBV
Not returning HttpResponse
3. Given this class-based view code, what will be the HTTP response content when a GET request is made?
from django.http import HttpResponse
from django.views import View
class HelloView(View):
def get(self, request):
return HttpResponse('Hello from CBV')
medium
A. HelloView object
B. Hello from CBV
C. Error: get method missing request
D. Empty response
Solution
Step 1: Identify the get method behavior
The get method returns HttpResponse with 'Hello from CBV'.
Step 2: Understand request handling
A GET request calls the get method and returns that response content.
Final Answer:
Hello from CBV -> Option B
Quick Check:
GET calls get() returning 'Hello from CBV' [OK]
Hint: GET calls get() method in CBV returning its HttpResponse [OK]
Common Mistakes:
Confusing class name with response content
Thinking get method lacks request parameter
Expecting empty or error response
4. What is wrong with this function-based view code?
def my_view():
return HttpResponse('Hi')
medium
A. Function name must be capitalized.
B. HttpResponse cannot be returned from a function.
C. Missing request parameter in function definition.
D. The return statement should be inside a class.
Solution
Step 1: Check function parameters
Function-based views must accept a request parameter to receive HTTP requests.
Step 2: Validate function signature
The given function lacks the required request parameter, causing errors.
Final Answer:
Missing request parameter in function definition. -> Option C
Quick Check:
FBV needs request param [OK]
Hint: FBVs always need request parameter [OK]
Common Mistakes:
Ignoring missing request parameter
Thinking HttpResponse can't be returned
Believing function names must be capitalized
5. You want to create a Django view that handles GET and POST requests differently and also reuse some common code for multiple views. Which approach is best?
hard
A. Use class-based views with methods for GET and POST and inheritance for reuse.
B. Use class-based views but define all logic in a single method.
C. Use function-based views with if-else inside to check request method.
D. Use function-based views with decorators for GET and POST.
Solution
Step 1: Identify need for handling GET and POST separately
Class-based views allow defining separate get() and post() methods for clarity.
Step 2: Consider code reuse
CBVs support inheritance, so common code can be reused across multiple views easily.
Final Answer:
Use class-based views with methods for GET and POST and inheritance for reuse. -> Option A
Quick Check:
CBVs = separate methods + reuse [OK]
Hint: CBVs separate methods and support inheritance for reuse [OK]