Bird
Raised Fist0
Djangoframework~20 mins

Function-based vs class-based decision in Django - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Django Views Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
Output of a function-based view with a redirect
What will be the HTTP status code returned by this Django function-based view when accessed?
Django
from django.http import HttpResponseRedirect

def my_view(request):
    return HttpResponseRedirect('/home/')
A302 Found
B404 Not Found
C200 OK
D500 Internal Server Error
Attempts:
2 left
💡 Hint
Think about what HttpResponseRedirect does in Django.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this class-based view
Which option correctly identifies the syntax error in this Django class-based view?
Django
from django.views import View
from django.http import HttpResponse

class MyView(View):
    def get(self, request):
        return HttpResponse('Hello')
    def post(self, request):
        return HttpResponse('Posted')
    def put(self, request):
        return HttpResponse('Put')
    def delete(self, request):
        return HttpResponse('Deleted')
    def patch(self, request):
        return HttpResponse('Patched')
    def options(self, request):
        return HttpResponse('Options')
    def trace(self, request):
        return HttpResponse('Trace')
    def head(self, request):
        return HttpResponse('Head')
    def connect(self, request):
        return HttpResponse('Connect')
    def get(self, request):
        return HttpResponse('Duplicate get')
ADuplicate method definition for 'get' in the class
BMissing colon after class declaration
CHttpResponse import is missing
DIndentation error in method definitions
Attempts:
2 left
💡 Hint
Look for repeated method names inside the class.
state_output
advanced
2:00remaining
State behavior difference between function-based and class-based views
Given these two views, what is the main difference in how they handle state between requests?
Django
from django.views import View
from django.http import HttpResponse

count = 0

def function_view(request):
    global count
    count += 1
    return HttpResponse(f'Count is {count}')

class ClassView(View):
    count = 0
    def get(self, request):
        ClassView.count += 1
        return HttpResponse(f'Count is {ClassView.count}')
AClass-based view count variable is instance-specific and resets each request
BBoth views share the same count variable across all requests
CFunction-based view resets count each request; class-based view does not
DFunction-based view uses a global variable; class-based view uses a class variable to track count
Attempts:
2 left
💡 Hint
Consider where the count variable is stored and how Python handles globals and class variables.
🔧 Debug
advanced
2:00remaining
Why does this class-based view raise an error?
This Django class-based view raises an error when accessed. What is the cause?
Django
from django.views import View
from django.http import HttpResponse

class MyView(View):
    def get(self):
        return HttpResponse('Hello')
AHttpResponse is not imported correctly
BMissing 'request' parameter in the 'get' method
CClass does not inherit from the correct base class
DMethod name 'get' is invalid in class-based views
Attempts:
2 left
💡 Hint
Check the method signature for class-based views.
🧠 Conceptual
expert
3:00remaining
Choosing between function-based and class-based views for complex logic
Which statement best explains why you might choose a class-based view over a function-based view in Django for a complex page?
AFunction-based views support middleware better than class-based views, so they are preferred for complex logic
BFunction-based views automatically handle all HTTP methods without extra code, so they are better for complex logic
CClass-based views allow you to organize related HTTP methods and reuse code through inheritance, making complex logic easier to manage
DClass-based views are faster to execute than function-based views, so they improve performance for complex pages
Attempts:
2 left
💡 Hint
Think about code organization and reuse in object-oriented programming.

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

  1. Step 1: Understand CBVs and inheritance

    Class-based views use classes, so they can inherit and reuse code easily.
  2. Step 2: Compare with FBVs

    Function-based views are simple functions and do not support inheritance for reuse.
  3. Final Answer:

    CBVs allow reuse of common functionality through inheritance. -> Option D
  4. 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

  1. Step 1: Check function signature for FBV

    A function-based view must accept a request parameter.
  2. Step 2: Validate return statement

    The function should return an HttpResponse object.
  3. Final Answer:

    def my_view(request): return HttpResponse('Hello') -> Option A
  4. 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

  1. Step 1: Identify the get method behavior

    The get method returns HttpResponse with 'Hello from CBV'.
  2. Step 2: Understand request handling

    A GET request calls the get method and returns that response content.
  3. Final Answer:

    Hello from CBV -> Option B
  4. 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

  1. Step 1: Check function parameters

    Function-based views must accept a request parameter to receive HTTP requests.
  2. Step 2: Validate function signature

    The given function lacks the required request parameter, causing errors.
  3. Final Answer:

    Missing request parameter in function definition. -> Option C
  4. 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

  1. Step 1: Identify need for handling GET and POST separately

    Class-based views allow defining separate get() and post() methods for clarity.
  2. Step 2: Consider code reuse

    CBVs support inheritance, so common code can be reused across multiple views easily.
  3. Final Answer:

    Use class-based views with methods for GET and POST and inheritance for reuse. -> Option A
  4. Quick Check:

    CBVs = separate methods + reuse [OK]
Hint: CBVs separate methods and support inheritance for reuse [OK]
Common Mistakes:
  • Using FBVs with complex if-else for methods
  • Putting all logic in one CBV method
  • Ignoring inheritance benefits